1 /*
2  * Copyright (C) 2012 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.settings.users;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 
26 import com.android.settings.Utils;
27 
28 
29 /**
30  * Watches for changes to Me Profile in Contacts and writes the photo to the User Manager.
31  */
32 public class ProfileUpdateReceiver extends BroadcastReceiver {
33 
34     private static final String KEY_PROFILE_NAME_COPIED_ONCE = "name_copied_once";
35 
36     @Override
onReceive(final Context context, Intent intent)37     public void onReceive(final Context context, Intent intent) {
38         // Profile changed, lets get the photo and write to user manager
39         new Thread() {
40             public void run() {
41                 Utils.copyMeProfilePhoto(context, null);
42                 copyProfileName(context);
43             }
44         }.start();
45     }
46 
copyProfileName(Context context)47     static void copyProfileName(Context context) {
48         SharedPreferences prefs = context.getSharedPreferences("profile", Context.MODE_PRIVATE);
49         if (prefs.contains(KEY_PROFILE_NAME_COPIED_ONCE)) {
50             return;
51         }
52 
53         int userId = UserHandle.myUserId();
54         UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
55         String profileName = Utils.getMeProfileName(context, false /* partial name */);
56         if (profileName != null && profileName.length() > 0) {
57             um.setUserName(userId, profileName);
58             // Flag that we've written the profile one time at least. No need to do it in the future.
59             prefs.edit().putBoolean(KEY_PROFILE_NAME_COPIED_ONCE, true).commit();
60         }
61     }
62 }
63