Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1303953d authored by Ethan Chen's avatar Ethan Chen
Browse files

Show owner info creation if no owner is set for UserTile

Forward port from cm-10.2.
Squash of the following commits:

Show owner info creation if no owner is set for UserTile

Change-Id: I43575946023676a1b75b67e1ae1996a0a3a35b88

Fix showing owner info creation from UserTile if no owner is set

* Turns out the default user will always be present, meaning a check of
  user count == 0 fails.

Change-Id: Id8e8e75c5f51c73a60d7b9039298ecb90e572bc9

Show default profile name if no profile exists

Change-Id: I6b651d6b552ab3caf30102d289469eaf68f58a0e

UserTile : Check if profile has a photo

In the event that the profile doesn't have a photo, don't default to the
user's photo, instead show a default avatar.

Change-Id: I80de324ae1e151693cf3716bf87d39a16096ca12
parent e7264a32
Loading
Loading
Loading
Loading
+52 −14
Original line number Diff line number Diff line
package com.android.systemui.quicksettings;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.app.ActivityManagerNative;
import android.content.Context;
import android.content.Intent;
@@ -7,13 +11,17 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.UserInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Profile;
import android.util.Log;
@@ -31,6 +39,7 @@ import com.android.systemui.statusbar.phone.QuickSettingsController;
public class UserTile extends QuickSettingsTile {

    private static final String TAG = "UserTile";
    private static final String INTENT_EXTRA_NEW_LOCAL_PROFILE = "newLocalProfile";
    private Drawable userAvatar;
    private AsyncTask<Void, Void, Pair<String, Drawable>> mUserInfoTask;

@@ -43,16 +52,26 @@ public class UserTile extends QuickSettingsTile {
                mQsc.mBar.collapseAllPanels(true);
                final UserManager um =
                        (UserManager) mContext.getSystemService(Context.USER_SERVICE);
                if (um.getUsers(true).size() > 1) {
                int numUsers = um.getUsers(true).size();
                if (numUsers <= 1) {
                    final Cursor cursor = mContext.getContentResolver().query(
                            Profile.CONTENT_URI, null, null, null, null);
                    if (cursor.moveToNext() && !cursor.isNull(0)) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, ContactsContract.Profile.CONTENT_URI);
                        startSettingsActivity(intent);
                    } else {
                        Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
                        intent.putExtra(INTENT_EXTRA_NEW_LOCAL_PROFILE, true);
                        startSettingsActivity(intent);
                    }
                    cursor.close();
                } else {
                    try {
                        WindowManagerGlobal.getWindowManagerService().lockNow(
                                null);
                    } catch (RemoteException e) {
                        Log.e(TAG, "Couldn't show user switcher", e);
                    }
                } else {
                    Intent intent = new Intent(Intent.ACTION_VIEW, ContactsContract.Profile.CONTENT_URI);
                    startSettingsActivity(intent);
                }
            }
        };
@@ -113,17 +132,9 @@ public class UserTile extends QuickSettingsTile {
                final UserManager um =
                        (UserManager) mContext.getSystemService(Context.USER_SERVICE);

                // Fall back to the UserManager nickname if we can't read the name from the local
                // profile below.
                String name = userName;
                String name = null;
                Drawable avatar = null;
                Bitmap rawAvatar = um.getUserIcon(userId);
                if (rawAvatar != null) {
                    avatar = new BitmapDrawable(mContext.getResources(), rawAvatar);
                } else {
                    avatar = mContext.getResources().getDrawable(R.drawable.ic_qs_default_user);
                }

                String id = null;
                // If it's a single-user device, get the profile name, since the nickname is not
                // usually valid
                if (um.getUsers().size() <= 1) {
@@ -135,10 +146,37 @@ public class UserTile extends QuickSettingsTile {
                        try {
                            if (cursor.moveToFirst()) {
                                name = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
                                id = cursor.getString(cursor.getColumnIndex(Phone._ID));
                            }
                        } finally {
                            cursor.close();
                        }
                        // Fall back to the UserManager nickname if we can't read the name from the local
                        // profile below.
                        if (name == null) {
                            avatar = mContext.getResources().getDrawable(R.drawable.ic_qs_default_user);
                            name = mContext.getResources().getString(com.android.internal.R.string.owner_name);
                        } else {
                            Bitmap rawAvatar = null;
                            InputStream is = null;
                            try {
                                Uri.Builder uriBuilder = ContactsContract.RawContacts.CONTENT_URI.buildUpon();
                                uriBuilder.appendPath(id);
                                uriBuilder.appendPath(Contacts.Photo.DISPLAY_PHOTO);
                                is = mContext.getContentResolver().openInputStream(uriBuilder.build());
                                rawAvatar = BitmapFactory.decodeStream(is);
                                avatar = new BitmapDrawable(mContext.getResources(), rawAvatar);
                            } catch (FileNotFoundException e) {
                                avatar = mContext.getResources().getDrawable(R.drawable.ic_qs_default_user);
                            } finally {
                                if (is != null) {
                                    try {
                                        is.close();
                                    } catch (IOException e) {
                                    }
                                }
                            }
                        }
                    }
                }
                return new Pair<String, Drawable>(name, avatar);