1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settings.applications; 17 18 import android.os.Handler; 19 import android.os.Looper; 20 import android.os.Message; 21 22 import com.android.settingslib.applications.ApplicationsState; 23 import com.android.settingslib.applications.ApplicationsState.AppEntry; 24 import com.android.settingslib.applications.ApplicationsState.Session; 25 26 import java.util.ArrayList; 27 28 /** 29 * Common base class for bridging information to ApplicationsState. 30 */ 31 public abstract class AppStateBaseBridge implements ApplicationsState.Callbacks { 32 33 protected final ApplicationsState mAppState; 34 protected final Session mAppSession; 35 protected final Callback mCallback; 36 protected final BackgroundHandler mHandler; 37 protected final MainHandler mMainHandler; 38 AppStateBaseBridge(ApplicationsState appState, Callback callback)39 public AppStateBaseBridge(ApplicationsState appState, Callback callback) { 40 mAppState = appState; 41 mAppSession = mAppState != null ? mAppState.newSession(this) : null; 42 mCallback = callback; 43 // Running on the same background thread as the ApplicationsState lets 44 // us run in the background and make sure they aren't doing updates at 45 // the same time as us as well. 46 mHandler = new BackgroundHandler(mAppState != null ? mAppState.getBackgroundLooper() 47 : Looper.getMainLooper()); 48 mMainHandler = new MainHandler(Looper.getMainLooper()); 49 } 50 resume()51 public void resume() { 52 mHandler.sendEmptyMessage(BackgroundHandler.MSG_LOAD_ALL); 53 mAppSession.onResume(); 54 } 55 pause()56 public void pause() { 57 mAppSession.onPause(); 58 } 59 release()60 public void release() { 61 mAppSession.onDestroy(); 62 } 63 forceUpdate(String pkg, int uid)64 public void forceUpdate(String pkg, int uid) { 65 mHandler.obtainMessage(BackgroundHandler.MSG_FORCE_LOAD_PKG, uid, 0, pkg).sendToTarget(); 66 } 67 68 @Override onPackageListChanged()69 public void onPackageListChanged() { 70 mHandler.sendEmptyMessage(BackgroundHandler.MSG_LOAD_ALL); 71 } 72 73 @Override onLoadEntriesCompleted()74 public void onLoadEntriesCompleted() { 75 mHandler.sendEmptyMessage(BackgroundHandler.MSG_LOAD_ALL); 76 } 77 78 @Override onRunningStateChanged(boolean running)79 public void onRunningStateChanged(boolean running) { 80 // No op. 81 } 82 83 @Override onRebuildComplete(ArrayList<AppEntry> apps)84 public void onRebuildComplete(ArrayList<AppEntry> apps) { 85 // No op. 86 } 87 88 @Override onPackageIconChanged()89 public void onPackageIconChanged() { 90 // No op. 91 } 92 93 @Override onPackageSizeChanged(String packageName)94 public void onPackageSizeChanged(String packageName) { 95 // No op. 96 } 97 98 @Override onAllSizesComputed()99 public void onAllSizesComputed() { 100 // No op. 101 } 102 103 @Override onLauncherInfoChanged()104 public void onLauncherInfoChanged() { 105 // No op. 106 } 107 loadAllExtraInfo()108 protected abstract void loadAllExtraInfo(); 109 updateExtraInfo(AppEntry app, String pkg, int uid)110 protected abstract void updateExtraInfo(AppEntry app, String pkg, int uid); 111 112 private class MainHandler extends Handler { 113 private static final int MSG_INFO_UPDATED = 1; 114 MainHandler(Looper looper)115 public MainHandler(Looper looper) { 116 super(looper); 117 } 118 119 @Override handleMessage(Message msg)120 public void handleMessage(Message msg) { 121 switch (msg.what) { 122 case MSG_INFO_UPDATED: 123 mCallback.onExtraInfoUpdated(); 124 break; 125 } 126 } 127 } 128 129 private class BackgroundHandler extends Handler { 130 private static final int MSG_LOAD_ALL = 1; 131 private static final int MSG_FORCE_LOAD_PKG = 2; 132 BackgroundHandler(Looper looper)133 public BackgroundHandler(Looper looper) { 134 super(looper); 135 } 136 137 @Override handleMessage(Message msg)138 public void handleMessage(Message msg) { 139 switch (msg.what) { 140 case MSG_LOAD_ALL: 141 loadAllExtraInfo(); 142 mMainHandler.sendEmptyMessage(MainHandler.MSG_INFO_UPDATED); 143 break; 144 case MSG_FORCE_LOAD_PKG: 145 ArrayList<AppEntry> apps = mAppSession.getAllApps(); 146 final int N = apps.size(); 147 String pkg = (String) msg.obj; 148 int uid = msg.arg1; 149 for (int i = 0; i < N; i++) { 150 AppEntry app = apps.get(i); 151 if (app.info.uid == uid && pkg.equals(app.info.packageName)) { 152 updateExtraInfo(app, pkg, uid); 153 } 154 } 155 mMainHandler.sendEmptyMessage(MainHandler.MSG_INFO_UPDATED); 156 break; 157 } 158 } 159 } 160 161 162 public interface Callback { onExtraInfoUpdated()163 void onExtraInfoUpdated(); 164 } 165 } 166