• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.getBackgroundLooper());
47         mMainHandler = new MainHandler();
48     }
49 
resume()50     public void resume() {
51         mHandler.sendEmptyMessage(BackgroundHandler.MSG_LOAD_ALL);
52         mAppSession.resume();
53     }
54 
pause()55     public void pause() {
56         mAppSession.pause();
57     }
58 
release()59     public void release() {
60         mAppSession.release();
61     }
62 
forceUpdate(String pkg, int uid)63     public void forceUpdate(String pkg, int uid) {
64         mHandler.obtainMessage(BackgroundHandler.MSG_FORCE_LOAD_PKG, uid, 0, pkg).sendToTarget();
65     }
66 
67     @Override
onPackageListChanged()68     public void onPackageListChanged() {
69         mHandler.sendEmptyMessage(BackgroundHandler.MSG_LOAD_ALL);
70     }
71 
72     @Override
onLoadEntriesCompleted()73     public void onLoadEntriesCompleted() {
74         mHandler.sendEmptyMessage(BackgroundHandler.MSG_LOAD_ALL);
75     }
76 
77     @Override
onRunningStateChanged(boolean running)78     public void onRunningStateChanged(boolean running) {
79         // No op.
80     }
81 
82     @Override
onRebuildComplete(ArrayList<AppEntry> apps)83     public void onRebuildComplete(ArrayList<AppEntry> apps) {
84         // No op.
85     }
86 
87     @Override
onPackageIconChanged()88     public void onPackageIconChanged() {
89         // No op.
90     }
91 
92     @Override
onPackageSizeChanged(String packageName)93     public void onPackageSizeChanged(String packageName) {
94         // No op.
95     }
96 
97     @Override
onAllSizesComputed()98     public void onAllSizesComputed() {
99         // No op.
100     }
101 
102     @Override
onLauncherInfoChanged()103     public void onLauncherInfoChanged() {
104         // No op.
105     }
106 
loadAllExtraInfo()107     protected abstract void loadAllExtraInfo();
updateExtraInfo(AppEntry app, String pkg, int uid)108     protected abstract void updateExtraInfo(AppEntry app, String pkg, int uid);
109 
110     private class MainHandler extends Handler {
111         private static final int MSG_INFO_UPDATED = 1;
112 
113         @Override
handleMessage(Message msg)114         public void handleMessage(Message msg) {
115             switch (msg.what) {
116                 case MSG_INFO_UPDATED:
117                     mCallback.onExtraInfoUpdated();
118                     break;
119             }
120         }
121     }
122 
123     private class BackgroundHandler extends Handler {
124         private static final int MSG_LOAD_ALL = 1;
125         private static final int MSG_FORCE_LOAD_PKG = 2;
126 
BackgroundHandler(Looper looper)127         public BackgroundHandler(Looper looper) {
128             super(looper);
129         }
130 
131         @Override
handleMessage(Message msg)132         public void handleMessage(Message msg) {
133             switch (msg.what) {
134                 case MSG_LOAD_ALL:
135                     loadAllExtraInfo();
136                     mMainHandler.sendEmptyMessage(MainHandler.MSG_INFO_UPDATED);
137                     break;
138                 case MSG_FORCE_LOAD_PKG:
139                     ArrayList<AppEntry> apps = mAppSession.getAllApps();
140                     final int N = apps.size();
141                     String pkg = (String) msg.obj;
142                     int uid = msg.arg1;
143                     for (int i = 0; i < N; i++) {
144                         AppEntry app = apps.get(i);
145                         if (app.info.uid == uid && pkg.equals(app.info.packageName)) {
146                             updateExtraInfo(app, pkg, uid);
147                         }
148                     }
149                     mMainHandler.sendEmptyMessage(MainHandler.MSG_INFO_UPDATED);
150                     break;
151             }
152         }
153     }
154 
155 
156     public interface Callback {
onExtraInfoUpdated()157         void onExtraInfoUpdated();
158     }
159 }
160