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 39 private boolean mForceLoadAllApps; 40 AppStateBaseBridge(ApplicationsState appState, Callback callback)41 public AppStateBaseBridge(ApplicationsState appState, Callback callback) { 42 mAppState = appState; 43 mAppSession = mAppState != null ? mAppState.newSession(this) : null; 44 mCallback = callback; 45 // Running on the same background thread as the ApplicationsState lets 46 // us run in the background and make sure they aren't doing updates at 47 // the same time as us as well. 48 mHandler = new BackgroundHandler(mAppState != null ? mAppState.getBackgroundLooper() 49 : Looper.getMainLooper()); 50 mMainHandler = new MainHandler(Looper.getMainLooper()); 51 } 52 resume(boolean forceLoadAllApps)53 public void resume(boolean forceLoadAllApps) { 54 mForceLoadAllApps = forceLoadAllApps; 55 if (mForceLoadAllApps) { 56 mAppSession.onResume(); 57 } else { 58 mAppSession.activateSession(); 59 } 60 } 61 pause()62 public void pause() { 63 if (mForceLoadAllApps) { 64 mAppSession.onPause(); 65 } else { 66 mAppSession.deactivateSession(); 67 } 68 } 69 release()70 public void release() { 71 mAppSession.onDestroy(); 72 } 73 forceUpdate(String pkg, int uid)74 public void forceUpdate(String pkg, int uid) { 75 mHandler.obtainMessage(BackgroundHandler.MSG_FORCE_LOAD_PKG, uid, 0, pkg).sendToTarget(); 76 } 77 78 @Override onPackageListChanged()79 public void onPackageListChanged() { 80 mHandler.sendEmptyMessage(BackgroundHandler.MSG_LOAD_ALL); 81 } 82 83 @Override onLoadEntriesCompleted()84 public void onLoadEntriesCompleted() { 85 mHandler.sendEmptyMessage(BackgroundHandler.MSG_LOAD_ALL); 86 } 87 88 @Override onRunningStateChanged(boolean running)89 public void onRunningStateChanged(boolean running) { 90 // No op. 91 } 92 93 @Override onRebuildComplete(ArrayList<AppEntry> apps)94 public void onRebuildComplete(ArrayList<AppEntry> apps) { 95 // No op. 96 } 97 98 @Override onPackageIconChanged()99 public void onPackageIconChanged() { 100 // No op. 101 } 102 103 @Override onPackageSizeChanged(String packageName)104 public void onPackageSizeChanged(String packageName) { 105 // No op. 106 } 107 108 @Override onAllSizesComputed()109 public void onAllSizesComputed() { 110 // No op. 111 } 112 113 @Override onLauncherInfoChanged()114 public void onLauncherInfoChanged() { 115 // No op. 116 } 117 loadAllExtraInfo()118 protected abstract void loadAllExtraInfo(); 119 updateExtraInfo(AppEntry app, String pkg, int uid)120 protected abstract void updateExtraInfo(AppEntry app, String pkg, int uid); 121 122 private class MainHandler extends Handler { 123 private static final int MSG_INFO_UPDATED = 1; 124 MainHandler(Looper looper)125 public MainHandler(Looper looper) { 126 super(looper); 127 } 128 129 @Override handleMessage(Message msg)130 public void handleMessage(Message msg) { 131 switch (msg.what) { 132 case MSG_INFO_UPDATED: 133 mCallback.onExtraInfoUpdated(); 134 break; 135 } 136 } 137 } 138 139 private class BackgroundHandler extends Handler { 140 private static final int MSG_LOAD_ALL = 1; 141 private static final int MSG_FORCE_LOAD_PKG = 2; 142 BackgroundHandler(Looper looper)143 public BackgroundHandler(Looper looper) { 144 super(looper); 145 } 146 147 @Override handleMessage(Message msg)148 public void handleMessage(Message msg) { 149 switch (msg.what) { 150 case MSG_LOAD_ALL: 151 loadAllExtraInfo(); 152 mMainHandler.sendEmptyMessage(MainHandler.MSG_INFO_UPDATED); 153 break; 154 case MSG_FORCE_LOAD_PKG: 155 ArrayList<AppEntry> apps = mAppSession.getAllApps(); 156 final int N = apps.size(); 157 String pkg = (String) msg.obj; 158 int uid = msg.arg1; 159 for (int i = 0; i < N; i++) { 160 AppEntry app = apps.get(i); 161 if (app.info.uid == uid && pkg.equals(app.info.packageName)) { 162 updateExtraInfo(app, pkg, uid); 163 } 164 } 165 mMainHandler.sendEmptyMessage(MainHandler.MSG_INFO_UPDATED); 166 break; 167 } 168 } 169 } 170 171 172 public interface Callback { onExtraInfoUpdated()173 void onExtraInfoUpdated(); 174 } 175 } 176