AppStateBaseBridge.java revision 67cd6ab93a526fe65877743e458590f4e6f187ee
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 */
16package com.android.settings.applications;
17
18import android.os.Handler;
19import android.os.Looper;
20import android.os.Message;
21
22import com.android.settingslib.applications.ApplicationsState;
23import com.android.settingslib.applications.ApplicationsState.AppEntry;
24import com.android.settingslib.applications.ApplicationsState.Session;
25
26import java.util.ArrayList;
27
28/**
29 * Common base class for bridging information to ApplicationsState.
30 */
31public 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    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
50    public void resume() {
51        mHandler.sendEmptyMessage(BackgroundHandler.MSG_LOAD_ALL);
52        mAppSession.resume();
53    }
54
55    public void pause() {
56        mAppSession.pause();
57    }
58
59    public void release() {
60        mAppSession.release();
61    }
62
63    public void forceUpdate(String pkg, int uid) {
64        mHandler.obtainMessage(BackgroundHandler.MSG_FORCE_LOAD_PKG, uid, 0, pkg).sendToTarget();
65    }
66
67    @Override
68    public void onPackageListChanged() {
69        mHandler.sendEmptyMessage(BackgroundHandler.MSG_LOAD_ALL);
70    }
71
72    @Override
73    public void onLoadEntriesCompleted() {
74        mHandler.sendEmptyMessage(BackgroundHandler.MSG_LOAD_ALL);
75    }
76
77    @Override
78    public void onRunningStateChanged(boolean running) {
79        // No op.
80    }
81
82    @Override
83    public void onRebuildComplete(ArrayList<AppEntry> apps) {
84        // No op.
85    }
86
87    @Override
88    public void onPackageIconChanged() {
89        // No op.
90    }
91
92    @Override
93    public void onPackageSizeChanged(String packageName) {
94        // No op.
95    }
96
97    @Override
98    public void onAllSizesComputed() {
99        // No op.
100    }
101
102    @Override
103    public void onLauncherInfoChanged() {
104        // No op.
105    }
106
107    protected abstract void loadAllExtraInfo();
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
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
127        public BackgroundHandler(Looper looper) {
128            super(looper);
129        }
130
131        @Override
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 {
157        void onExtraInfoUpdated();
158    }
159}
160