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