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 17 package com.android.tv.settings.users; 18 19 import android.app.Service; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.SharedPreferences; 23 import android.os.IBinder; 24 import android.os.UserHandle; 25 26 /** 27 * Service for storing the exit pin for restricted profiles. This service is only to be used by the 28 * {@link RestrictedProfilePinStorage} 29 * 30 * The pin is stored in the Settings app's shared preferences of the system user. This is only 31 * accessible to the Settings app in the system user, which makes it a safe place for storing 32 * the pin. 33 */ 34 public class RestrictedProfilePinService extends Service { 35 private static final String TAG = RestrictedProfilePinService.class.getSimpleName(); 36 37 public static final String PIN_STORE_NAME = "restricted_profile_pin"; 38 39 private IRestrictedProfilePinService.Stub mBinder; 40 41 @Override onCreate()42 public void onCreate() { 43 mBinder = isSystemUser() ? new PinServiceImpl(this) : null; 44 } 45 46 @Override onBind(Intent intent)47 public IBinder onBind(Intent intent) { 48 return mBinder; 49 } 50 51 @Override onDestroy()52 public void onDestroy() { 53 mBinder = null; 54 } 55 isSystemUser()56 private boolean isSystemUser() { 57 return UserHandle.myUserId() == UserHandle.USER_SYSTEM; 58 } 59 60 private static class PinServiceImpl extends IRestrictedProfilePinService.Stub { 61 private SharedPreferences mSharedPref; 62 PinServiceImpl(Context context)63 PinServiceImpl(Context context) { 64 mSharedPref = context.getSharedPreferences(PIN_STORE_NAME, Context.MODE_PRIVATE); 65 } 66 67 @Override isPinCorrect(String pin)68 public boolean isPinCorrect(String pin) { 69 String savedPin = getPin(); 70 return pin.equals(savedPin); 71 } 72 73 @Override setPin(String pin)74 public void setPin(String pin) { 75 mSharedPref.edit() 76 .putString(PIN_STORE_NAME, pin) 77 .apply(); 78 } 79 80 @Override deletePin()81 public void deletePin() { 82 mSharedPref.edit() 83 .remove(PIN_STORE_NAME) 84 .apply(); 85 } 86 87 @Override isPinSet()88 public boolean isPinSet() { 89 String savedPin = getPin(); 90 return savedPin != null; 91 } 92 getPin()93 private String getPin() { 94 return mSharedPref.getString(PIN_STORE_NAME, null); 95 } 96 } 97 } 98