132dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani/* 232dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * Copyright (C) 2012 The Android Open Source Project 332dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * 432dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * Licensed under the Apache License, Version 2.0 (the "License"); 532dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * you may not use this file except in compliance with the License. 632dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * You may obtain a copy of the License at 732dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * 832dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * http://www.apache.org/licenses/LICENSE-2.0 932dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * 1032dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * Unless required by applicable law or agreed to in writing, software 1132dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * distributed under the License is distributed on an "AS IS" BASIS, 1232dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1332dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * See the License for the specific language governing permissions and 1432dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * limitations under the License. 1532dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani */ 1632dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani 1732dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasanipackage com.android.settings.users; 1832dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani 1932dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasaniimport android.content.BroadcastReceiver; 2032dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasaniimport android.content.Context; 2132dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasaniimport android.content.Intent; 22ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasaniimport android.content.SharedPreferences; 2332dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasaniimport android.os.UserHandle; 2432dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasaniimport android.os.UserManager; 2539b467482d1bf256a111c757e9b7621c6f523271Jason Monk 26ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasaniimport com.android.settings.Utils; 2732dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani 2832dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani 2932dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani/** 3032dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * Watches for changes to Me Profile in Contacts and writes the photo to the User Manager. 3132dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani */ 3232dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasanipublic class ProfileUpdateReceiver extends BroadcastReceiver { 3332dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani 34ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani private static final String KEY_PROFILE_NAME_COPIED_ONCE = "name_copied_once"; 35ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani 3632dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani @Override 3732dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani public void onReceive(final Context context, Intent intent) { 3832dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani // Profile changed, lets get the photo and write to user manager 3932dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani new Thread() { 4032dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani public void run() { 41ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani Utils.copyMeProfilePhoto(context, null); 42ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani copyProfileName(context); 4332dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani } 4432dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani }.start(); 4532dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani } 4632dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani 47ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani static void copyProfileName(Context context) { 48ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani SharedPreferences prefs = context.getSharedPreferences("profile", Context.MODE_PRIVATE); 49ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani if (prefs.contains(KEY_PROFILE_NAME_COPIED_ONCE)) { 50ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani return; 5132dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani } 52ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani 53ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani int userId = UserHandle.myUserId(); 5432dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); 558d40fac706d206355f9a4be58744fb1a8cc3417dAmith Yamasani String profileName = Utils.getMeProfileName(context, false /* partial name */); 56ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani if (profileName != null && profileName.length() > 0) { 57ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani um.setUserName(userId, profileName); 58ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani // Flag that we've written the profile one time at least. No need to do it in the future. 59ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani prefs.edit().putBoolean(KEY_PROFILE_NAME_COPIED_ONCE, true).commit(); 6032dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani } 6132dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani } 6232dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani} 63