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

Commit 23baea9b authored by Fan Zhang's avatar Fan Zhang
Browse files

Remove unused fields and move ProfileSelect to Settings

Bug: 77600770
Test: rebuild
Change-Id: I34bbf888324901f707fba13274196a4b1ca846da
parent f5d74c76
Loading
Loading
Loading
Loading
+1 −32
Original line number Diff line number Diff line
@@ -24,26 +24,15 @@ import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class DashboardCategory implements Parcelable {

    /**
     * Title of the category that is shown to the user.
     */
    public CharSequence title;

    /**
     * Key used for placing external tiles.
     */
    public String key;

    /**
     * Used to control display order.
     */
    public int priority;

    /**
     * List of the category's children
     */
@@ -75,14 +64,6 @@ public class DashboardCategory implements Parcelable {
        mTiles.add(tile);
    }

    public synchronized void addTile(int n, Tile tile) {
        mTiles.add(n, tile);
    }

    public synchronized void removeTile(Tile tile) {
        mTiles.remove(tile);
    }

    public synchronized void removeTile(int n) {
        mTiles.remove(n);
    }
@@ -99,7 +80,7 @@ public class DashboardCategory implements Parcelable {
     * Sort priority value for tiles in this category.
     */
    public void sortTiles() {
        Collections.sort(mTiles, TILE_COMPARATOR);
        Collections.sort(mTiles, Tile.TILE_COMPARATOR);
    }

    /**
@@ -136,9 +117,7 @@ public class DashboardCategory implements Parcelable {

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        TextUtils.writeToParcel(title, dest, flags);
        dest.writeString(key);
        dest.writeInt(priority);

        final int count = mTiles.size();
        dest.writeInt(count);
@@ -150,9 +129,7 @@ public class DashboardCategory implements Parcelable {
    }

    public void readFromParcel(Parcel in) {
        title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
        key = in.readString();
        priority = in.readInt();

        final int count = in.readInt();

@@ -173,12 +150,4 @@ public class DashboardCategory implements Parcelable {
        }
    };

    public static final Comparator<Tile> TILE_COMPARATOR =
            new Comparator<Tile>() {
                @Override
                public int compare(Tile lhs, Tile rhs) {
                    return rhs.priority - lhs.priority;
                }
            };

}
+0 −90
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settingslib.drawer;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;

import java.util.List;

public class ProfileSelectDialog extends DialogFragment implements OnClickListener {

    private static final String TAG = "ProfileSelectDialog";
    private static final String ARG_SELECTED_TILE = "selectedTile";
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

    private Tile mSelectedTile;

    public static void show(FragmentManager manager, Tile tile) {
        ProfileSelectDialog dialog = new ProfileSelectDialog();
        Bundle args = new Bundle();
        args.putParcelable(ARG_SELECTED_TILE, tile);
        dialog.setArguments(args);
        dialog.show(manager, "select_profile");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSelectedTile = getArguments().getParcelable(ARG_SELECTED_TILE);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Context context = getActivity();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        UserAdapter adapter = UserAdapter.createUserAdapter(UserManager.get(context), context,
                mSelectedTile.userHandle);
        builder.setTitle(com.android.settingslib.R.string.choose_profile)
                .setAdapter(adapter, this);

        return builder.create();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        UserHandle user = mSelectedTile.userHandle.get(which);
        // Show menu on top level items.
        mSelectedTile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        getActivity().startActivityAsUser(mSelectedTile.intent, user);
    }

    public static void updateUserHandlesIfNeeded(Context context, Tile tile) {
        List<UserHandle> userHandles = tile.userHandle;
        if (tile.userHandle == null || tile.userHandle.size() <= 1) {
            return;
        }
        final UserManager userManager = UserManager.get(context);
        for (int i = userHandles.size() - 1; i >= 0; i--) {
            if (userManager.getUserInfo(userHandles.get(i).getIdentifier()) == null) {
                if (DEBUG) {
                    Log.d(TAG, "Delete the user: " + userHandles.get(i).getIdentifier());
                }
                userHandles.remove(i);
            }
        }
    }
}
+4 −7
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.os.UserHandle;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
@@ -74,11 +75,6 @@ public class Tile implements Parcelable {
     */
    public ArrayList<UserHandle> userHandle = new ArrayList<>();

    /**
     * Optional additional data for use by subclasses of the activity
     */
    public Bundle extras;

    /**
     * Category in which the tile should be placed.
     */
@@ -132,7 +128,6 @@ public class Tile implements Parcelable {
        for (int i = 0; i < N; i++) {
            userHandle.get(i).writeToParcel(dest, flags);
        }
        dest.writeBundle(extras);
        dest.writeString(category);
        dest.writeInt(priority);
        dest.writeBundle(metaData);
@@ -179,7 +174,6 @@ public class Tile implements Parcelable {
        for (int i = 0; i < N; i++) {
            userHandle.add(UserHandle.CREATOR.createFromParcel(in));
        }
        extras = in.readBundle();
        category = in.readString();
        priority = in.readInt();
        metaData = in.readBundle();
@@ -216,4 +210,7 @@ public class Tile implements Parcelable {
        profile = (profile != null ? profile : PROFILE_ALL);
        return TextUtils.equals(profile, PROFILE_PRIMARY);
    }

    public static final Comparator<Tile> TILE_COMPARATOR =
            (lhs, rhs) -> rhs.priority - lhs.priority;
}
+20 −33
Original line number Diff line number Diff line
@@ -37,8 +37,6 @@ import android.util.Pair;
import androidx.annotation.VisibleForTesting;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -76,7 +74,6 @@ public class TileUtils {
    private static final String IA_SETTINGS_ACTION =
            "com.android.settings.action.IA_SETTINGS";


    /**
     * Same as #EXTRA_SETTINGS_ACTION but used for the platform Settings activities.
     */
@@ -199,6 +196,7 @@ public class TileUtils {

    /**
     * Build a list of DashboardCategory.
     *
     * @param extraAction additional intent filter action to be usetileutild to build the dashboard
     *                    categories
     */
@@ -247,9 +245,11 @@ public class TileUtils {
        for (DashboardCategory category : categories) {
            category.sortTiles();
        }
        Collections.sort(categories, CATEGORY_COMPARATOR);
        if (DEBUG_TIMING) Log.d(LOG_TAG, "getCategories took "

        if (DEBUG_TIMING) {
            Log.d(LOG_TAG, "getCategories took "
                    + (System.currentTimeMillis() - startTime) + " ms");
        }
        return categories;
    }

@@ -269,13 +269,13 @@ public class TileUtils {
            intent.setPackage(SETTING_PKG);
        }
        getTilesForIntent(context, user, intent, addedCache, defaultCategory, outTiles,
                usePriority, true, true);
                usePriority, true);
    }

    public static void getTilesForIntent(
            Context context, UserHandle user, Intent intent,
            Map<Pair<String, String>, Tile> addedCache, String defaultCategory, List<Tile> outTiles,
            boolean usePriority, boolean checkCategory, boolean forceTintExternalIcon) {
            boolean usePriority, boolean checkCategory) {
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
                PackageManager.GET_META_DATA, user.getIdentifier());
@@ -308,8 +308,7 @@ public class TileUtils {
                tile.category = categoryKey;
                tile.priority = usePriority ? resolved.priority : 0;
                tile.metaData = activityInfo.metaData;
                updateTileData(context, tile, activityInfo, activityInfo.applicationInfo,
                        pm, forceTintExternalIcon);
                updateTileData(context, tile, activityInfo, activityInfo.applicationInfo, pm);
                if (DEBUG) Log.d(LOG_TAG, "Adding tile " + tile.title);
                addedCache.put(key, tile);
            }
@@ -324,8 +323,7 @@ public class TileUtils {
    }

    private static boolean updateTileData(Context context, Tile tile,
            ActivityInfo activityInfo, ApplicationInfo applicationInfo, PackageManager pm,
            boolean forceTintExternalIcon) {
            ActivityInfo activityInfo, ApplicationInfo applicationInfo, PackageManager pm) {
        if (applicationInfo.isSystemApp()) {
            boolean forceTintIcon = false;
            CharSequence title = null;
@@ -338,8 +336,7 @@ public class TileUtils {
                Resources res = pm.getResourcesForApplication(applicationInfo.packageName);
                Bundle metaData = activityInfo.metaData;

                if (forceTintExternalIcon
                        && !context.getPackageName().equals(applicationInfo.packageName)) {
                if (!context.getPackageName().equals(applicationInfo.packageName)) {
                    isIconTintable = true;
                    forceTintIcon = true;
                }
@@ -403,6 +400,7 @@ public class TileUtils {

    /**
     * Gets the icon package name and resource id from content provider.
     *
     * @param context     context
     * @param packageName package name of the target activity
     * @param uriString   URI for the content provider
@@ -433,6 +431,7 @@ public class TileUtils {

    /**
     * Gets text associated with the input key from the content provider.
     *
     * @param context     context
     * @param uriString   URI for the content provider
     * @param providerMap Maps URI authorities to providers
@@ -466,10 +465,6 @@ public class TileUtils {
        }
    }

    private static String getString(Bundle bundle, String key) {
        return bundle == null ? null : bundle.getString(key);
    }

    private static IContentProvider getProviderFromUri(Context context, Uri uri,
            Map<String, IContentProvider> providerMap) {
        if (uri == null) {
@@ -496,12 +491,4 @@ public class TileUtils {
        }
        return pathSegments.get(0);
    }

    private static final Comparator<DashboardCategory> CATEGORY_COMPARATOR =
            new Comparator<DashboardCategory>() {
        @Override
        public int compare(DashboardCategory lhs, DashboardCategory rhs) {
            return rhs.priority - lhs.priority;
        }
    };
}
+0 −213
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.drawer;

import android.app.ActivityManager;

import android.content.Context;
import android.content.pm.UserInfo;
import android.database.DataSetObserver;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.os.UserManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import com.android.internal.util.UserIcons;
import com.android.settingslib.drawable.UserIconDrawable;

import com.android.settingslib.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Adapter for a spinner that shows a list of users.
 */
public class UserAdapter implements SpinnerAdapter, ListAdapter {
    /** Holder for user details */
    public static class UserDetails {
        private final UserHandle mUserHandle;
        private final String mName;
        private final Drawable mIcon;

        public UserDetails(UserHandle userHandle, UserManager um, Context context) {
            mUserHandle = userHandle;
            UserInfo userInfo = um.getUserInfo(mUserHandle.getIdentifier());
            Drawable icon;
            if (userInfo.isManagedProfile()) {
                mName = context.getString(R.string.managed_user_title);
                icon = context.getDrawable(
                    com.android.internal.R.drawable.ic_corp_badge);
            } else {
                mName = userInfo.name;
                final int userId = userInfo.id;
                if (um.getUserIcon(userId) != null) {
                    icon = new BitmapDrawable(context.getResources(), um.getUserIcon(userId));
                } else {
                    icon = UserIcons.getDefaultUserIcon(
                            context.getResources(), userId, /* light= */ false);
                }
            }
            this.mIcon = encircle(context, icon);
        }

        private static Drawable encircle(Context context, Drawable icon) {
            return new UserIconDrawable(UserIconDrawable.getSizeForList(context))
                    .setIconDrawable(icon).bake();
        }
    }
    private ArrayList<UserDetails> data;
    private final LayoutInflater mInflater;

    public UserAdapter(Context context, ArrayList<UserDetails> users) {
        if (users == null) {
            throw new IllegalArgumentException("A list of user details must be provided");
        }
        this.data = users;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public UserHandle getUserHandle(int position) {
        if (position < 0 || position >= data.size()) {
            return null;
        }
        return data.get(position).mUserHandle;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        final View row = convertView != null ? convertView : createUser(parent);

        UserDetails user = data.get(position);
        ((ImageView) row.findViewById(android.R.id.icon)).setImageDrawable(user.mIcon);
        ((TextView) row.findViewById(android.R.id.title)).setText(getTitle(user));
        return row;
    }

    private int getTitle(UserDetails user) {
        int userHandle = user.mUserHandle.getIdentifier();
        if (userHandle == UserHandle.USER_CURRENT
                || userHandle == ActivityManager.getCurrentUser()) {
            return R.string.category_personal;
        } else {
            return R.string.category_work;
        }
    }

    private View createUser(ViewGroup parent) {
        return mInflater.inflate(R.layout.user_preference, parent, false);
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        // We don't support observers
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        // We don't support observers
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public UserDetails getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return data.get(position).mUserHandle.getIdentifier();
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getDropDownView(position, convertView, parent);
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return data.isEmpty();
    }

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    @Override
    public boolean isEnabled(int position) {
        return true;
    }

    /**
     * Creates a {@link UserAdapter} if there is more than one profile on the device.
     *
     * <p> The adapter can be used to populate a spinner that switches between the Settings
     * app on the different profiles.
     *
     * @return a {@link UserAdapter} or null if there is only one profile.
     */
    public static UserAdapter createUserSpinnerAdapter(UserManager userManager,
            Context context) {
        List<UserHandle> userProfiles = userManager.getUserProfiles();
        if (userProfiles.size() < 2) {
            return null;
        }

        UserHandle myUserHandle = new UserHandle(UserHandle.myUserId());
        // The first option should be the current profile
        userProfiles.remove(myUserHandle);
        userProfiles.add(0, myUserHandle);

        return createUserAdapter(userManager, context, userProfiles);
    }

    public static UserAdapter createUserAdapter(UserManager userManager,
            Context context, List<UserHandle> userProfiles) {
        ArrayList<UserDetails> userDetails = new ArrayList<UserDetails>(userProfiles.size());
        final int count = userProfiles.size();
        for (int i = 0; i < count; i++) {
            userDetails.add(new UserDetails(userProfiles.get(i), userManager, context));
        }
        return new UserAdapter(context, userDetails);
    }
}
Loading