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;
25ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasaniimport android.provider.ContactsContract.CommonDataKinds.Phone;
26ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani
27ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasaniimport com.android.settings.Utils;
2832dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani
2932dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani
3032dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani/**
3132dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * Watches for changes to Me Profile in Contacts and writes the photo to the User Manager.
3232dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani */
3332dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasanipublic class ProfileUpdateReceiver extends BroadcastReceiver {
3432dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani
35ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani    private static final String KEY_PROFILE_NAME_COPIED_ONCE = "name_copied_once";
36ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani
3732dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani    @Override
3832dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani    public void onReceive(final Context context, Intent intent) {
3932dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani        // Profile changed, lets get the photo and write to user manager
4032dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani        new Thread() {
4132dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani            public void run() {
42ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani                Utils.copyMeProfilePhoto(context, null);
43ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani                copyProfileName(context);
4432dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani            }
4532dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani        }.start();
4632dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani    }
4732dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani
48ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani    static void copyProfileName(Context context) {
49ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani        SharedPreferences prefs = context.getSharedPreferences("profile", Context.MODE_PRIVATE);
50ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani        if (prefs.contains(KEY_PROFILE_NAME_COPIED_ONCE)) {
51ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani            return;
5232dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani        }
53ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani
54ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani        int userId = UserHandle.myUserId();
5532dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani        UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
568d40fac706d206355f9a4be58744fb1a8cc3417dAmith Yamasani        String profileName = Utils.getMeProfileName(context, false /* partial name */);
57ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani        if (profileName != null && profileName.length() > 0) {
58ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani            um.setUserName(userId, profileName);
59ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani            // Flag that we've written the profile one time at least. No need to do it in the future.
60ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani            prefs.edit().putBoolean(KEY_PROFILE_NAME_COPIED_ONCE, true).commit();
6132dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani        }
6232dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani    }
6332dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani}
64