From a42004f47da3d2f8128f77b7170bf84dd86d7e19 Mon Sep 17 00:00:00 2001 From: Gabriele M Date: Mon, 27 Mar 2017 17:23:04 +0200 Subject: [PATCH] CustomTiles: Add system profiles tile This tile opens a dialog to change the current profile. If system profiles are disabled, they'll be enabled as soon as a profile is selected. Disabling system profiles from the tile is not possible. Change-Id: I38b28be8fcf1eb7998d7d18df204802ea66554f6 --- AndroidManifest.xml | 11 ++ res/drawable/ic_profiles.xml | 28 +++++ res/values/strings.xml | 4 + src/org/lineageos/customtiles/MainActivity.java | 1 + src/org/lineageos/customtiles/ProfileTile.java | 139 ++++++++++++++++++++++++ 5 files changed, 183 insertions(+) create mode 100644 res/drawable/ic_profiles.xml create mode 100644 src/org/lineageos/customtiles/ProfileTile.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 6bef3c7..09f2de9 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -129,6 +129,17 @@ + + + + + + + + + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 60cb829..e0dd75e 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -24,8 +24,12 @@ Sync Volume panel USB tethering + System Profile Caffeine + Select profile + No system profile found + diff --git a/src/org/lineageos/customtiles/MainActivity.java b/src/org/lineageos/customtiles/MainActivity.java index 95d3015..4f9f3ba 100644 --- a/src/org/lineageos/customtiles/MainActivity.java +++ b/src/org/lineageos/customtiles/MainActivity.java @@ -33,6 +33,7 @@ public class MainActivity extends Activity { put(".SyncTile", new Intent(Settings.ACTION_SYNC_SETTINGS)); put(".UsbTetherTile", new Intent(Settings.ACTION_WIRELESS_SETTINGS)); put(".VolumePanelTile", new Intent(Settings.ACTION_SOUND_SETTINGS)); + put(".ProfileTile", new Intent("org.cyanogenmod.cmparts.PROFILES_SETTINGS")); }}; @Override diff --git a/src/org/lineageos/customtiles/ProfileTile.java b/src/org/lineageos/customtiles/ProfileTile.java new file mode 100644 index 0000000..ed3caab --- /dev/null +++ b/src/org/lineageos/customtiles/ProfileTile.java @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2017 The LineageOS 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 org.lineageos.customtiles; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.IntentFilter; +import android.service.quicksettings.Tile; +import android.service.quicksettings.TileService; + +import java.util.UUID; + +import cyanogenmod.app.Profile; +import cyanogenmod.app.ProfileManager; +import cyanogenmod.providers.CMSettings; + +public class ProfileTile extends TileService { + + private ProfileManager mProfileManager; + + private Profile mCurrentProfile; + + private BroadcastReceiver mReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + refresh(); + } + }; + + @Override + public void onStartListening() { + super.onStartListening(); + + mProfileManager = ProfileManager.getInstance(getBaseContext()); + + IntentFilter filter = new IntentFilter(); + filter.addAction(ProfileManager.INTENT_ACTION_PROFILE_SELECTED); + filter.addAction(ProfileManager.INTENT_ACTION_PROFILE_UPDATED); + filter.addAction(ProfileManager.PROFILES_STATE_CHANGED_ACTION); + registerReceiver(mReceiver, filter); + + refresh(); + } + + @Override + public void onStopListening() { + unregisterReceiver(mReceiver); + super.onStopListening(); + } + + private Dialog selectProfileDialog() { + + Profile[] profilesList = mProfileManager.getProfiles(); + if (profilesList.length == 0) { + return new AlertDialog.Builder(getBaseContext()) + .setTitle(R.string.dialog_system_profile_title) + .setMessage(R.string.dialog_system_profile_message) + .setPositiveButton(android.R.string.ok, null) + .create(); + } + + final CharSequence[] profileLabels = new CharSequence[profilesList.length]; + final UUID[] profileUuids = new UUID[profilesList.length]; + int selectedProfile = -1; + for (int i = 0; i < profilesList.length; i++) { + UUID profileUuuid = profilesList[i].getUuid(); + profileLabels[i] = profilesList[i].getName(); + profileUuids[i] = profileUuuid; + if (mCurrentProfile != null && mCurrentProfile.getUuid().equals(profileUuuid)) { + selectedProfile = i; + } + } + + return new AlertDialog.Builder(getBaseContext()) + .setTitle(R.string.dialog_system_profile_title) + .setSingleChoiceItems(profileLabels, selectedProfile, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int index) { + if (!mProfileManager.isProfilesEnabled()) { + enableProfiles(); + } + mProfileManager.setActiveProfile(profileUuids[index]); + dialogInterface.dismiss(); + } + }) + .setNegativeButton(android.R.string.cancel, null) + .setCancelable(true) + .create(); + } + + @Override + public void onClick() { + super.onClick(); + + unlockAndRun(new Runnable() { + @Override + public void run() { + showDialog(selectProfileDialog()); + } + }); + } + + private void refresh() { + if (mProfileManager.isProfilesEnabled()) { + mCurrentProfile = mProfileManager.getActiveProfile(); + getQsTile().setState(Tile.STATE_ACTIVE); + getQsTile().setLabel(mCurrentProfile.getName()); + } else { + mCurrentProfile = null; + getQsTile().setState(Tile.STATE_INACTIVE); + getQsTile().setLabel(getString(R.string.system_profile_label)); + } + getQsTile().updateTile(); + } + + private void enableProfiles() { + CMSettings.System.putInt(getContentResolver(), + CMSettings.System.SYSTEM_PROFILES_ENABLED, 1); + } +} -- 2.13.1