1 /*
2  * Copyright (C) 2019 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.systemui.theme;
17 
18 import android.app.ActivityManager;
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.content.om.OverlayManager;
24 import android.content.pm.UserInfo;
25 import android.database.ContentObserver;
26 import android.net.Uri;
27 import android.os.AsyncTask;
28 import android.os.Handler;
29 import android.os.UserHandle;
30 import android.os.UserManager;
31 import android.provider.Settings;
32 import android.text.TextUtils;
33 import android.util.ArrayMap;
34 import android.util.Log;
35 
36 import com.android.systemui.R;
37 import com.android.systemui.SystemUI;
38 import com.android.systemui.broadcast.BroadcastDispatcher;
39 import com.android.systemui.dagger.qualifiers.Background;
40 
41 import com.google.android.collect.Sets;
42 
43 import org.json.JSONException;
44 import org.json.JSONObject;
45 
46 import java.util.Collection;
47 import java.util.Map;
48 import java.util.Set;
49 
50 import javax.inject.Inject;
51 import javax.inject.Singleton;
52 
53 /**
54  * Controls the application of theme overlays across the system for all users.
55  * This service is responsible for:
56  * - Observing changes to Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES and applying the
57  * corresponding overlays across the system
58  * - Observing user switches, applying the overlays for the current user to user 0 (for systemui)
59  * - Observing work profile changes and applying overlays from the primary user to their
60  * associated work profiles
61  */
62 @Singleton
63 public class ThemeOverlayController extends SystemUI {
64     private static final String TAG = "ThemeOverlayController";
65     private static final boolean DEBUG = false;
66 
67     private ThemeOverlayManager mThemeManager;
68     private UserManager mUserManager;
69     private BroadcastDispatcher mBroadcastDispatcher;
70     private final Handler mBgHandler;
71 
72     @Inject
ThemeOverlayController(Context context, BroadcastDispatcher broadcastDispatcher, @Background Handler bgHandler)73     public ThemeOverlayController(Context context, BroadcastDispatcher broadcastDispatcher,
74             @Background Handler bgHandler) {
75         super(context);
76         mBroadcastDispatcher = broadcastDispatcher;
77         mBgHandler = bgHandler;
78     }
79 
80     @Override
start()81     public void start() {
82         if (DEBUG) Log.d(TAG, "Start");
83         mUserManager = mContext.getSystemService(UserManager.class);
84         mThemeManager = new ThemeOverlayManager(
85                 mContext.getSystemService(OverlayManager.class),
86                 AsyncTask.THREAD_POOL_EXECUTOR,
87                 mContext.getString(R.string.launcher_overlayable_package),
88                 mContext.getString(R.string.themepicker_overlayable_package));
89         final IntentFilter filter = new IntentFilter();
90         filter.addAction(Intent.ACTION_USER_SWITCHED);
91         filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
92         mBroadcastDispatcher.registerReceiverWithHandler(new BroadcastReceiver() {
93             @Override
94             public void onReceive(Context context, Intent intent) {
95                 if (DEBUG) Log.d(TAG, "Updating overlays for user switch / profile added.");
96                 updateThemeOverlays();
97             }
98         }, filter, mBgHandler, UserHandle.ALL);
99         mContext.getContentResolver().registerContentObserver(
100                 Settings.Secure.getUriFor(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES),
101                 false,
102                 new ContentObserver(mBgHandler) {
103 
104                     @Override
105                     public void onChange(boolean selfChange, Collection<Uri> uris, int flags,
106                             int userId) {
107                         if (DEBUG) Log.d(TAG, "Overlay changed for user: " + userId);
108                         if (ActivityManager.getCurrentUser() == userId) {
109                             updateThemeOverlays();
110                         }
111                     }
112                 },
113                 UserHandle.USER_ALL);
114     }
115 
updateThemeOverlays()116     private void updateThemeOverlays() {
117         final int currentUser = ActivityManager.getCurrentUser();
118         final String overlayPackageJson = Settings.Secure.getStringForUser(
119                 mContext.getContentResolver(), Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES,
120                 currentUser);
121         if (DEBUG) Log.d(TAG, "updateThemeOverlays: " + overlayPackageJson);
122         final Map<String, String> categoryToPackage = new ArrayMap<>();
123         if (!TextUtils.isEmpty(overlayPackageJson)) {
124             try {
125                 JSONObject object = new JSONObject(overlayPackageJson);
126                 for (String category : ThemeOverlayManager.THEME_CATEGORIES) {
127                     if (object.has(category)) {
128                         categoryToPackage.put(category, object.getString(category));
129                     }
130                 }
131             } catch (JSONException e) {
132                 Log.i(TAG, "Failed to parse THEME_CUSTOMIZATION_OVERLAY_PACKAGES.", e);
133             }
134         }
135         Set<UserHandle> userHandles = Sets.newHashSet(UserHandle.of(currentUser));
136         for (UserInfo userInfo : mUserManager.getEnabledProfiles(currentUser)) {
137             if (userInfo.isManagedProfile()) {
138                 userHandles.add(userInfo.getUserHandle());
139             }
140         }
141         mThemeManager.applyCurrentUserOverlays(categoryToPackage, userHandles);
142     }
143 }
144