• 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.ActivityManagerNative;
20 import android.app.Service;
21 import android.app.SynchronousUserSwitchObserver;
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 import com.android.tv.settings.system.SecurityFragment;
35 
36 public class UserSwitchListenerService extends Service {
37 
38     private static final boolean DEBUG = false;
39     private static final String TAG = "RestrictedProfile";
40 
41     private static final String RESTRICTED_PROFILE_LAUNCHER_ENTRY_ACTIVITY =
42             "com.android.tv.settings.users.RestrictedProfileActivityLauncherEntry";
43     private static final String SHARED_PREFERENCES_NAME = "RestrictedProfileSharedPreferences";
44     private static final String
45             ON_BOOT_USER_ID_PREFERENCE = "UserSwitchOnBootBroadcastReceiver.userId";
46 
47     public static class BootReceiver extends BroadcastReceiver {
48         @Override
onReceive(final Context context, Intent intent)49         public void onReceive(final Context context, Intent intent) {
50 
51             boolean isSystemUser = UserManager.get(context).isSystemUser();
52 
53             if (isSystemUser) {
54                 context.startService(new Intent(context, UserSwitchListenerService.class));
55                 int bootUserId = getBootUser(context);
56                 if (DEBUG) {
57                     Log.d(TAG,
58                             "boot completed, user is " + UserHandle.myUserId()
59                             + " boot user id: "
60                             + bootUserId);
61                 }
62                 if (UserHandle.myUserId() != bootUserId) {
63                     switchUserNow(bootUserId);
64                 }
65             }
66 
67             updateLaunchPoint(context, null != SecurityFragment.findRestrictedUser(
68                     (UserManager) context.getSystemService(Context.USER_SERVICE)));
69         }
70     }
71 
updateLaunchPoint(Context context, boolean enableLaunchPoint)72     public static void updateLaunchPoint(Context context, boolean enableLaunchPoint) {
73         if (DEBUG) {
74             Log.d(TAG, "updating launch point: " + enableLaunchPoint);
75         }
76 
77         PackageManager pm = context.getPackageManager();
78         ComponentName compName = new ComponentName(context,
79                 RESTRICTED_PROFILE_LAUNCHER_ENTRY_ACTIVITY);
80         pm.setComponentEnabledSetting(compName,
81                 enableLaunchPoint ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
82                         : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
83                 PackageManager.DONT_KILL_APP);
84     }
85 
setBootUser(Context context, int userId)86     static void setBootUser(Context context, int userId) {
87         SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
88                 Context.MODE_PRIVATE).edit();
89         editor.putInt(ON_BOOT_USER_ID_PREFERENCE, userId);
90         editor.apply();
91     }
92 
getBootUser(Context context)93     static int getBootUser(Context context) {
94         SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
95                 Context.MODE_PRIVATE);
96         return prefs.getInt(ON_BOOT_USER_ID_PREFERENCE, UserHandle.USER_SYSTEM);
97     }
98 
switchUserNow(int userId)99     private static void switchUserNow(int userId) {
100         try {
101             ActivityManagerNative.getDefault().switchUser(userId);
102         } catch (RemoteException re) {
103             Log.e(TAG, "Caught exception while switching user! ", re);
104         }
105     }
106 
107     @Override
onCreate()108     public void onCreate() {
109         super.onCreate();
110         try {
111             ActivityManagerNative.getDefault().registerUserSwitchObserver(
112                     new SynchronousUserSwitchObserver() {
113                         @Override
114                         public void onUserSwitching(int newUserId) throws RemoteException {
115                         }
116 
117                         @Override
118                         public void onUserSwitchComplete(int newUserId) throws RemoteException {
119                             if (DEBUG) {
120                                 Log.d(TAG, "user has been foregrounded: " + newUserId);
121                             }
122                             setBootUser(UserSwitchListenerService.this, newUserId);
123                         }
124 
125                         @Override
126                         public void onForegroundProfileSwitch(int profileId)
127                             throws RemoteException {
128                         }
129                     }, UserSwitchListenerService.class.getName());
130         } catch (RemoteException e) {
131         }
132     }
133 
134     @Override
onStartCommand(Intent intent, int flags, int startId)135     public int onStartCommand(Intent intent, int flags, int startId) {
136         return START_STICKY;
137     }
138 
139     @Override
onBind(Intent intent)140     public IBinder onBind(Intent intent) {
141         return null;
142     }
143 }
144