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