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.Dependency;
37 import com.android.systemui.R;
38 import com.android.systemui.SystemUI;
39 
40 import com.google.android.collect.Sets;
41 
42 import org.json.JSONException;
43 import org.json.JSONObject;
44 
45 import java.util.Map;
46 import java.util.Set;
47 
48 /**
49  * Controls the application of theme overlays across the system for all users.
50  * This service is responsible for:
51  * - Observing changes to Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES and applying the
52  * corresponding overlays across the system
53  * - Observing user switches, applying the overlays for the current user to user 0 (for systemui)
54  * - Observing work profile changes and applying overlays from the primary user to their
55  * associated work profiles
56  */
57 public class ThemeOverlayController extends SystemUI {
58     private static final String TAG = "ThemeOverlayController";
59     private static final boolean DEBUG = false;
60 
61     private ThemeOverlayManager mThemeManager;
62     private UserManager mUserManager;
63 
64     @Override
start()65     public void start() {
66         if (DEBUG) Log.d(TAG, "Start");
67         mUserManager = mContext.getSystemService(UserManager.class);
68         mThemeManager = new ThemeOverlayManager(
69                 mContext.getSystemService(OverlayManager.class),
70                 AsyncTask.THREAD_POOL_EXECUTOR,
71                 mContext.getString(R.string.launcher_overlayable_package),
72                 mContext.getString(R.string.themepicker_overlayable_package));
73         final Handler bgHandler = Dependency.get(Dependency.BG_HANDLER);
74         final IntentFilter filter = new IntentFilter();
75         filter.addAction(Intent.ACTION_USER_SWITCHED);
76         filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
77         mContext.registerReceiverAsUser(new BroadcastReceiver() {
78             @Override
79             public void onReceive(Context context, Intent intent) {
80                 if (DEBUG) Log.d(TAG, "Updating overlays for user switch / profile added.");
81                 updateThemeOverlays();
82             }
83         }, UserHandle.ALL, filter, null, bgHandler);
84         mContext.getContentResolver().registerContentObserver(
85                 Settings.Secure.getUriFor(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES),
86                 false,
87                 new ContentObserver(bgHandler) {
88                     @Override
89                     public void onChange(boolean selfChange, Uri uri, int userId) {
90                         if (DEBUG) Log.d(TAG, "Overlay changed for user: " + userId);
91                         if (ActivityManager.getCurrentUser() == userId) {
92                             updateThemeOverlays();
93                         }
94                     }
95                 },
96                 UserHandle.USER_ALL);
97     }
98 
updateThemeOverlays()99     private void updateThemeOverlays() {
100         final int currentUser = ActivityManager.getCurrentUser();
101         final String overlayPackageJson = Settings.Secure.getStringForUser(
102                 mContext.getContentResolver(), Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES,
103                 currentUser);
104         if (DEBUG) Log.d(TAG, "updateThemeOverlays: " + overlayPackageJson);
105         final Map<String, String> categoryToPackage = new ArrayMap<>();
106         if (!TextUtils.isEmpty(overlayPackageJson)) {
107             try {
108                 JSONObject object = new JSONObject(overlayPackageJson);
109                 for (String category : ThemeOverlayManager.THEME_CATEGORIES) {
110                     if (object.has(category)) {
111                         categoryToPackage.put(category, object.getString(category));
112                     }
113                 }
114             } catch (JSONException e) {
115                 Log.i(TAG, "Failed to parse THEME_CUSTOMIZATION_OVERLAY_PACKAGES.", e);
116             }
117         }
118         Set<UserHandle> userHandles = Sets.newHashSet(UserHandle.of(currentUser));
119         for (UserInfo userInfo : mUserManager.getEnabledProfiles(currentUser)) {
120             if (userInfo.isManagedProfile()) {
121                 userHandles.add(userInfo.getUserHandle());
122             }
123         }
124         mThemeManager.applyCurrentUserOverlays(categoryToPackage, userHandles);
125     }
126 }
127