• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 
17 package com.android.tv.settings.users;
18 
19 import android.app.ActivityManager;
20 import android.app.Service;
21 import android.app.UserSwitchObserver;
22 import android.content.BroadcastReceiver;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.SharedPreferences;
27 import android.content.pm.PackageManager;
28 import android.os.IBinder;
29 import android.os.RemoteException;
30 import android.os.UserHandle;
31 import android.os.UserManager;
32 import android.util.Log;
33 
34 public class UserSwitchListenerService extends Service {
35 
36     private static final boolean DEBUG = false;
37     private static final String TAG = "RestrictedProfile";
38 
39     private static final String RESTRICTED_PROFILE_LAUNCHER_ENTRY_ACTIVITY =
40             "com.android.tv.settings.users.RestrictedProfileActivityLauncherEntry";
41     private static final String SHARED_PREFERENCES_NAME = "RestrictedProfileSharedPreferences";
42     private static final String
43             ON_BOOT_USER_ID_PREFERENCE = "UserSwitchOnBootBroadcastReceiver.userId";
44 
45     public static class BootReceiver extends BroadcastReceiver {
46         @Override
onReceive(final Context context, Intent intent)47         public void onReceive(final Context context, Intent intent) {
48             boolean isSystemUser = UserManager.get(context).isSystemUser();
49             if (isSystemUser) {
50                 context.startService(new Intent(context, UserSwitchListenerService.class));
51                 int bootUserId = getBootUser(context);
52                 if (DEBUG) {
53                     Log.d(TAG, "boot completed, user is " + UserHandle.myUserId()
54                             + " boot user id: " + bootUserId);
55                 }
56                 if (UserHandle.myUserId() != bootUserId) {
57                     switchUserNow(bootUserId);
58                 }
59             }
60             updateLaunchPoint(context, new RestrictedProfileModel(context).getUser() != null);
61         }
62     }
63 
updateLaunchPoint(Context context, boolean enableLaunchPoint)64     public static void updateLaunchPoint(Context context, boolean enableLaunchPoint) {
65         if (DEBUG) {
66             Log.d(TAG, "updating launch point: " + enableLaunchPoint);
67         }
68 
69         PackageManager pm = context.getPackageManager();
70         ComponentName compName = new ComponentName(context,
71                 RESTRICTED_PROFILE_LAUNCHER_ENTRY_ACTIVITY);
72         pm.setComponentEnabledSetting(compName,
73                 enableLaunchPoint ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
74                         : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
75                 PackageManager.DONT_KILL_APP);
76     }
77 
setBootUser(Context context, int userId)78     static void setBootUser(Context context, int userId) {
79         SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
80                 Context.MODE_PRIVATE).edit();
81         editor.putInt(ON_BOOT_USER_ID_PREFERENCE, userId);
82         editor.apply();
83     }
84 
getBootUser(Context context)85     static int getBootUser(Context context) {
86         SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
87                 Context.MODE_PRIVATE);
88         return prefs.getInt(ON_BOOT_USER_ID_PREFERENCE, UserHandle.USER_SYSTEM);
89     }
90 
switchUserNow(int userId)91     private static void switchUserNow(int userId) {
92         try {
93             ActivityManager.getService().switchUser(userId);
94         } catch (RemoteException re) {
95             Log.e(TAG, "Caught exception while switching user! ", re);
96         }
97     }
98 
99     @Override
onCreate()100     public void onCreate() {
101         super.onCreate();
102         try {
103             ActivityManager.getService().registerUserSwitchObserver(
104                     new UserSwitchObserver() {
105                         @Override
106                         public void onUserSwitchComplete(int newUserId) throws RemoteException {
107                             if (DEBUG) {
108                                 Log.d(TAG, "user has been foregrounded: " + newUserId);
109                             }
110                             setBootUser(UserSwitchListenerService.this, newUserId);
111                         }
112                     }, UserSwitchListenerService.class.getName());
113         } catch (RemoteException e) {
114         }
115     }
116 
117     @Override
onDestroy()118     public void onDestroy() {
119         super.onDestroy();
120     }
121 
122     @Override
onStartCommand(Intent intent, int flags, int startId)123     public int onStartCommand(Intent intent, int flags, int startId) {
124         return START_STICKY;
125     }
126 
127     @Override
onBind(Intent intent)128     public IBinder onBind(Intent intent) {
129         return null;
130     }
131 }
132