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

Commit 12e0a09a authored by Sven Dawitz's avatar Sven Dawitz Committed by Ricardo Cerqueira
Browse files

QuickSettings: Added ProfileTile (2/2)

- Adds the option for a Profile Tile to System settings
- Profile tile will show selected profile
- On click a profile selection will pop up

Patchset 2:
  Using blundens profile png resource
  fixing broadcast
  adding longClick action to updated system settings
Patchset 3:
  only show tile, if profiles are enabled
Patchset 4:
  removed reduntant check
  removed reduntant parameter from constructor

Change-Id: I74548af63531a245c700cc5b50f97919de7cfcf8
parent 6d837a9d
Loading
Loading
Loading
Loading
+3.51 KiB
Loading image diff...
+3.22 KiB
Loading image diff...
+3.81 KiB
Loading image diff...
+2 −0
Original line number Diff line number Diff line
@@ -495,6 +495,8 @@
    <string name="quick_settings_wifi_display_label">Wi-Fi Display</string>
    <!-- QuickSettings: Wifi display [CHAR LIMIT=NONE] -->
    <string name="quick_settings_wifi_display_no_connection_label">Wireless Display</string>
    <!-- QuickSettings: Profile [CHAR LIMIT=NONE] -->
    <string name="quick_settings_profile_label">Profile</string>
    <!-- QuickSettings: Brightness dialog title [CHAR LIMIT=NONE] -->
    <string name="quick_settings_brightness_dialog_title">Brightness</string>
    <!-- QuickSettings: Brightness dialog auto brightness button [CHAR LIMIT=NONE] -->
+149 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 Sven Dawitz for the CyanogenMod 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.systemui.quicksettings;

import android.app.AlertDialog;
import android.app.Profile;
import android.app.ProfileManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.android.server.ProfileManagerService;
import com.android.systemui.R;
import com.android.systemui.statusbar.phone.QuickSettingsContainerView;
import com.android.systemui.statusbar.phone.QuickSettingsController;

import java.util.UUID;

public class ProfileTile extends QuickSettingsTile {
    private Profile mChosenProfile;
    private ProfileManager mProfileManager;
    private ProfileReceiver mProfileReceiver;

    public ProfileTile(Context context, LayoutInflater inflater,
            QuickSettingsContainerView container,
            QuickSettingsController qsc) {
        super(context, inflater, container, qsc);

        mProfileReceiver = new ProfileReceiver();
        mProfileReceiver.registerSelf();
        mProfileManager = (ProfileManager) mContext.getSystemService(Context.PROFILE_SERVICE);
        mDrawable = R.drawable.ic_qs_profiles;

        mLabel = mProfileManager.getActiveProfile().getName();

        mOnClick = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createProfileDialog();
            }
        };
        mOnLongClick = new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Intent intent = new Intent("android.settings.PROFILES_SETTINGS");
                startSettingsActivity(intent);
                return true;
            }
        };
    }

    private class ProfileReceiver extends BroadcastReceiver {
        private boolean mIsRegistered;

        public ProfileReceiver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            mLabel = mProfileManager.getActiveProfile().getName();
            updateQuickSettings();
        }

        private void registerSelf() {
            if (!mIsRegistered) {
                mIsRegistered = true;

                IntentFilter filter = new IntentFilter();
                filter.addAction(ProfileManagerService.INTENT_ACTION_PROFILE_SELECTED);
                mContext.registerReceiver(mProfileReceiver, filter);
            }
        }

        private void unregisterSelf() {
            if (mIsRegistered) {
                mIsRegistered = false;
                mContext.unregisterReceiver(this);
            }
        }
    }

    // copied from com.android.internal.policy.impl.GlobalActions
    private void createProfileDialog() {
        final ProfileManager profileManager = (ProfileManager) mContext
                .getSystemService(Context.PROFILE_SERVICE);

        final Profile[] profiles = profileManager.getProfiles();
        UUID activeProfile = profileManager.getActiveProfile().getUuid();
        final CharSequence[] names = new CharSequence[profiles.length];

        int i = 0;
        int checkedItem = 0;

        for (Profile profile : profiles) {
            if (profile.getUuid().equals(activeProfile)) {
                checkedItem = i;
                mChosenProfile = profile;
            }
            names[i++] = profile.getName();
        }

        final AlertDialog.Builder ab = new AlertDialog.Builder(mContext);

        AlertDialog dialog = ab
                .setTitle(R.string.quick_settings_profile_label)
                .setSingleChoiceItems(names, checkedItem, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (which < 0)
                            return;
                        mChosenProfile = profiles[which];
                    }
                })
                .setPositiveButton(com.android.internal.R.string.yes,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                profileManager.setActiveProfile(mChosenProfile.getUuid());
                            }
                        })
                .setNegativeButton(com.android.internal.R.string.no,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        }).create();
        mStatusbarService.animateCollapsePanels();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
        dialog.show();
    }

}
Loading