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 com.android.settings.Utils;
2632dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani
2732dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani
2832dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani/**
2932dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani * Watches for changes to Me Profile in Contacts and writes the photo to the User Manager.
3032dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani */
3132dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasanipublic class ProfileUpdateReceiver extends BroadcastReceiver {
3232dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani
33ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani    private static final String KEY_PROFILE_NAME_COPIED_ONCE = "name_copied_once";
34ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani
3532dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani    @Override
3632dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani    public void onReceive(final Context context, Intent intent) {
3732dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani        // Profile changed, lets get the photo and write to user manager
3832dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani        new Thread() {
3932dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani            public void run() {
40ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani                Utils.copyMeProfilePhoto(context, null);
41ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani                copyProfileName(context);
4232dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani            }
4332dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani        }.start();
4432dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani    }
4532dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani
46ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani    static void copyProfileName(Context context) {
47ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani        SharedPreferences prefs = context.getSharedPreferences("profile", Context.MODE_PRIVATE);
48ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani        if (prefs.contains(KEY_PROFILE_NAME_COPIED_ONCE)) {
49ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani            return;
5032dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani        }
51ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani
52ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani        int userId = UserHandle.myUserId();
5332dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani        UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
548d40fac706d206355f9a4be58744fb1a8cc3417dAmith Yamasani        String profileName = Utils.getMeProfileName(context, false /* partial name */);
55ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani        if (profileName != null && profileName.length() > 0) {
56ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani            um.setUserName(userId, profileName);
57ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani            // Flag that we've written the profile one time at least. No need to do it in the future.
58ae47ef43fa9f7f408ec08b92d4d334b159a68e82Amith Yamasani            prefs.edit().putBoolean(KEY_PROFILE_NAME_COPIED_ONCE, true).commit();
5932dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani        }
6032dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani    }
6132dccbc23fb905e109599389a7bed291b2a6385dAmith Yamasani}
62