• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.storagemanager.deletionhelper;
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();
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();
updateExtraInfo(AppEntry app, String pkg, int uid)109     protected abstract void updateExtraInfo(AppEntry app, String pkg, int uid);
110 
111     private class MainHandler extends Handler {
112         private static final int MSG_INFO_UPDATED = 1;
113 
114         @Override
handleMessage(Message msg)115         public void handleMessage(Message msg) {
116             switch (msg.what) {
117                 case MSG_INFO_UPDATED:
118                     mCallback.onExtraInfoUpdated();
119                     break;
120             }
121         }
122     }
123 
124     private class BackgroundHandler extends Handler {
125         private static final int MSG_LOAD_ALL = 1;
126         private static final int MSG_FORCE_LOAD_PKG = 2;
127 
BackgroundHandler(Looper looper)128         public BackgroundHandler(Looper looper) {
129             super(looper);
130         }
131 
132         @Override
handleMessage(Message msg)133         public void handleMessage(Message msg) {
134             switch (msg.what) {
135                 case MSG_LOAD_ALL:
136                     loadAllExtraInfo();
137                     mMainHandler.sendEmptyMessage(MainHandler.MSG_INFO_UPDATED);
138                     break;
139                 case MSG_FORCE_LOAD_PKG:
140                     ArrayList<AppEntry> apps = mAppSession.getAllApps();
141                     final int N = apps.size();
142                     String pkg = (String) msg.obj;
143                     int uid = msg.arg1;
144                     for (int i = 0; i < N; i++) {
145                         AppEntry app = apps.get(i);
146                         if (app.info.uid == uid && pkg.equals(app.info.packageName)) {
147                             updateExtraInfo(app, pkg, uid);
148                         }
149                     }
150                     mMainHandler.sendEmptyMessage(MainHandler.MSG_INFO_UPDATED);
151                     break;
152             }
153         }
154     }
155 
156 
157     public interface Callback {
onExtraInfoUpdated()158         void onExtraInfoUpdated();
159     }
160 }
161