/*
* Copyright (C) 2012 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.settings.cyanogenmod;
import static com.android.internal.util.cm.QSConstants.TILES_DEFAULT;
import static com.android.internal.util.cm.QSConstants.TILE_AIRPLANE;
import static com.android.internal.util.cm.QSConstants.TILE_AUTOROTATE;
import static com.android.internal.util.cm.QSConstants.TILE_BATTERY;
import static com.android.internal.util.cm.QSConstants.TILE_BLUETOOTH;
import static com.android.internal.util.cm.QSConstants.TILE_BRIGHTNESS;
import static com.android.internal.util.cm.QSConstants.TILE_DELIMITER;
import static com.android.internal.util.cm.QSConstants.TILE_GPS;
import static com.android.internal.util.cm.QSConstants.TILE_LOCKSCREEN;
import static com.android.internal.util.cm.QSConstants.TILE_LTE;
import static com.android.internal.util.cm.QSConstants.TILE_MOBILEDATA;
import static com.android.internal.util.cm.QSConstants.TILE_NETWORKMODE;
import static com.android.internal.util.cm.QSConstants.TILE_NFC;
import static com.android.internal.util.cm.QSConstants.TILE_PROFILE;
import static com.android.internal.util.cm.QSConstants.TILE_QUIETHOURS;
import static com.android.internal.util.cm.QSConstants.TILE_RINGER;
import static com.android.internal.util.cm.QSConstants.TILE_SCREENTIMEOUT;
import static com.android.internal.util.cm.QSConstants.TILE_SETTINGS;
import static com.android.internal.util.cm.QSConstants.TILE_SLEEP;
import static com.android.internal.util.cm.QSConstants.TILE_SYNC;
import static com.android.internal.util.cm.QSConstants.TILE_TORCH;
import static com.android.internal.util.cm.QSConstants.TILE_USER;
import static com.android.internal.util.cm.QSConstants.TILE_VOLUME;
import static com.android.internal.util.cm.QSConstants.TILE_WIFI;
import static com.android.internal.util.cm.QSConstants.TILE_WIFIAP;
import static com.android.internal.util.cm.QSUtils.deviceSupportsBluetooth;
import static com.android.internal.util.cm.QSUtils.deviceSupportsTelephony;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;
import com.android.settings.R;
public class QuickSettingsUtil {
// Keep sorted according to titleResId's string value
public static final LinkedHashMap<String, TileInfo> TILES = new LinkedHashMap<String, TileInfo>();
static {
TILES.put(TILE_AIRPLANE, new QuickSettingsUtil.TileInfo(
TILE_AIRPLANE, R.string.title_tile_airplane,
"com.android.systemui:drawable/ic_qs_airplane_off"));
TILES.put(TILE_BATTERY, new QuickSettingsUtil.TileInfo(
TILE_BATTERY, R.string.title_tile_battery,
"com.android.systemui:drawable/ic_qs_battery_neutral"));
TILES.put(TILE_BLUETOOTH, new QuickSettingsUtil.TileInfo(
TILE_BLUETOOTH, R.string.title_tile_bluetooth,
"com.android.systemui:drawable/ic_qs_bluetooth_neutral"));
TILES.put(TILE_BRIGHTNESS, new QuickSettingsUtil.TileInfo(
TILE_BRIGHTNESS, R.string.title_tile_brightness,
"com.android.systemui:drawable/ic_qs_brightness_auto_off"));
TILES.put(TILE_SLEEP, new QuickSettingsUtil.TileInfo(
TILE_SLEEP, R.string.title_tile_sleep,
"com.android.systemui:drawable/ic_qs_sleep"));
TILES.put(TILE_GPS, new QuickSettingsUtil.TileInfo(
TILE_GPS, R.string.title_tile_gps,
"com.android.systemui:drawable/ic_qs_gps_neutral"));
TILES.put(TILE_LOCKSCREEN, new QuickSettingsUtil.TileInfo(
TILE_LOCKSCREEN, R.string.title_tile_lockscreen,
"com.android.systemui:drawable/ic_qs_lock_screen_off"));
TILES.put(TILE_LTE, new QuickSettingsUtil.TileInfo(
TILE_LTE, R.string.title_tile_lte,
"com.android.systemui:drawable/ic_qs_lte_off"));
TILES.put(TILE_MOBILEDATA, new QuickSettingsUtil.TileInfo(
TILE_MOBILEDATA, R.string.title_tile_mobiledata,
"com.android.systemui:drawable/ic_qs_signal_4"));
TILES.put(TILE_NETWORKMODE, new QuickSettingsUtil.TileInfo(
TILE_NETWORKMODE, R.string.title_tile_networkmode,
"com.android.systemui:drawable/ic_qs_2g3g_on"));
TILES.put(TILE_NFC, new QuickSettingsUtil.TileInfo(
TILE_NFC, R.string.title_tile_nfc,
"com.android.systemui:drawable/ic_qs_nfc_off"));
TILES.put(TILE_AUTOROTATE, new QuickSettingsUtil.TileInfo(
TILE_AUTOROTATE, R.string.title_tile_autorotate,
"com.android.systemui:drawable/ic_qs_auto_rotate"));
TILES.put(TILE_PROFILE, new QuickSettingsUtil.TileInfo(
TILE_PROFILE, R.string.title_tile_profile,
"com.android.systemui:drawable/ic_qs_profiles"));
TILES.put(TILE_QUIETHOURS, new QuickSettingsUtil.TileInfo(
TILE_QUIETHOURS, R.string.title_tile_quiet_hours,
"com.android.systemui:drawable/ic_qs_quiet_hours_off"));
TILES.put(TILE_SETTINGS, new QuickSettingsUtil.TileInfo(
TILE_SETTINGS, R.string.title_tile_settings,
"com.android.systemui:drawable/ic_qs_settings"));
TILES.put(TILE_SLEEP, new QuickSettingsUtil.TileInfo(
TILE_SLEEP, R.string.title_tile_sleep,
"com.android.systemui:drawable/ic_qs_sleep"));
TILES.put(TILE_RINGER, new QuickSettingsUtil.TileInfo(
TILE_RINGER, R.string.title_tile_sound,
"com.android.systemui:drawable/ic_qs_ring_on"));
TILES.put(TILE_SYNC, new QuickSettingsUtil.TileInfo(
TILE_SYNC, R.string.title_tile_sync,
"com.android.systemui:drawable/ic_qs_sync_off"));
TILES.put(TILE_SCREENTIMEOUT, new QuickSettingsUtil.TileInfo(
TILE_SCREENTIMEOUT, R.string.title_tile_screen_timeout,
"com.android.systemui:drawable/ic_qs_screen_timeout_off"));
TILES.put(TILE_TORCH, new QuickSettingsUtil.TileInfo(
TILE_TORCH, R.string.title_tile_torch,
"com.android.systemui:drawable/ic_qs_torch_off"));
TILES.put(TILE_VOLUME, new QuickSettingsUtil.TileInfo(
TILE_VOLUME, R.string.title_tile_volume,
"com.android.systemui:drawable/ic_qs_volume"));
TILES.put(TILE_WIFI, new QuickSettingsUtil.TileInfo(
TILE_WIFI, R.string.title_tile_wifi,
"com.android.systemui:drawable/ic_qs_wifi_4"));
TILES.put(TILE_WIFIAP, new QuickSettingsUtil.TileInfo(
TILE_WIFIAP, R.string.title_tile_wifiap,
"com.android.systemui:drawable/ic_qs_wifi_ap_neutral"));
TILES.put(TILE_USER, new QuickSettingsUtil.TileInfo(
TILE_USER, R.string.title_tile_user,
"com.android.systemui:drawable/ic_qs_default_user"));
}
public static String getCurrentTiles(Context context) {
String tiles = Settings.System.getString(context.getContentResolver(),
Settings.System.QUICK_SETTINGS_TILES);
if (tiles == null) {
tiles = getDefaultTiles(context);
}
return tiles;
}
public static void saveCurrentTiles(Context context, String tiles) {
Settings.System.putString(context.getContentResolver(),
Settings.System.QUICK_SETTINGS_TILES, tiles);
}
public static void resetTiles(Context context) {
String defaultTiles = getDefaultTiles(context);
Settings.System.putString(context.getContentResolver(),
Settings.System.QUICK_SETTINGS_TILES, defaultTiles);
}
public static String mergeInNewTileString(String oldString, String newString) {
ArrayList<String> oldList = getTileListFromString(oldString);
ArrayList<String> newList = getTileListFromString(newString);
ArrayList<String> mergedList = new ArrayList<String>();
// add any items from oldlist that are in new list
for (String tile : oldList) {
if (newList.contains(tile)) {
mergedList.add(tile);
}
}
// append anything in newlist that isn't already in the merged list to
// the end of the list
for (String tile : newList) {
if (!mergedList.contains(tile)) {
mergedList.add(tile);
}
}
// return merged list
return getTileStringFromList(mergedList);
}
public static ArrayList<String> getTileListFromString(String tiles) {
return new ArrayList<String>(Arrays.asList(tiles.split("\\|")));
}
public static String getTileStringFromList(ArrayList<String> tiles) {
if (tiles == null || tiles.size() <= 0) {
return "";
} else {
String s = tiles.get(0);
for (int i = 1; i < tiles.size(); i++) {
s += TILE_DELIMITER + tiles.get(i);
}
return s;
}
}
public static String getDefaultTiles(Context context) {
// Filter items not compatible with device
boolean bluetoothSupported = deviceSupportsBluetooth();
boolean telephonySupported = deviceSupportsTelephony(context);
if (!bluetoothSupported) {
TILES_DEFAULT.remove(TILE_BLUETOOTH);
}
if (!telephonySupported) {
TILES_DEFAULT.remove(TILE_MOBILEDATA);
}
return TextUtils.join(TILE_DELIMITER, TILES_DEFAULT);
}
public static class TileInfo {
private String mId;
private int mTitleResId;
private String mIcon;
public TileInfo(String id, int titleResId, String icon) {
mId = id;
mTitleResId = titleResId;
mIcon = icon;
}
public String getId() {
return mId;
}
public int getTitleResId() {
return mTitleResId;
}
public String getIcon() {
return mIcon;
}
}
}