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