summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings
diff options
context:
space:
mode:
authorJo De Boeck <deboeck.jo@gmail.com>2013-06-06 21:22:26 +0200
committerDanny Baumann <dannybaumann@web.de>2013-06-14 15:36:32 +0200
commitf80eeea5dfee99cc48743165070bf4c9ba061d86 (patch)
tree36aea9e4f130ff2b9451fbe5495816bc3346d7ce /src/com/android/settings
parent278619db25a83ebc31a414ab010359a689bed3c4 (diff)
downloadpackages_apps_settings-f80eeea5dfee99cc48743165070bf4c9ba061d86.zip
packages_apps_settings-f80eeea5dfee99cc48743165070bf4c9ba061d86.tar.gz
packages_apps_settings-f80eeea5dfee99cc48743165070bf4c9ba061d86.tar.bz2
[2/2] Add triggers to change profile based on AP
Add UI for configuring triggers to active profiles. Change-Id: I4903ef64d3eced687136a7dcf4d2048ce4b7ca63
Diffstat (limited to 'src/com/android/settings')
-rw-r--r--src/com/android/settings/profiles/ProfileConfig.java22
-rw-r--r--src/com/android/settings/profiles/WifiTriggerAPPreference.java70
-rw-r--r--src/com/android/settings/profiles/WifiTriggerFragment.java139
3 files changed, 229 insertions, 2 deletions
diff --git a/src/com/android/settings/profiles/ProfileConfig.java b/src/com/android/settings/profiles/ProfileConfig.java
index fc47ff0..a27baa7 100644
--- a/src/com/android/settings/profiles/ProfileConfig.java
+++ b/src/com/android/settings/profiles/ProfileConfig.java
@@ -35,7 +35,6 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.net.wimax.WimaxHelper;
-import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
@@ -64,6 +63,8 @@ public class ProfileConfig extends SettingsPreferenceFragment
private static final int MENU_DELETE = Menu.FIRST + 1;
+ private static final int MENU_WIFI = Menu.FIRST + 2;
+
private Profile mProfile;
private NamePreference mNamePreference;
@@ -128,12 +129,16 @@ public class ProfileConfig extends SettingsPreferenceFragment
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
- if (NfcAdapter.getDefaultAdapter(getActivity()) != null) {
+ if (deviceSupportsNfc(getActivity())) {
MenuItem nfc = menu.add(0, MENU_NFC_WRITE, 0, R.string.profile_write_nfc_tag)
.setIcon(R.drawable.ic_menu_nfc_writer_dark);
nfc.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
+ MenuItem wifi = menu.add(0, MENU_WIFI, 0, R.string.profile_trigger_wifi)
+ .setIcon(R.drawable.ic_location);
+ wifi.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
+ MenuItem.SHOW_AS_ACTION_WITH_TEXT);
MenuItem delete = menu.add(0, MENU_DELETE, 1, R.string.profile_menu_delete)
.setIcon(R.drawable.ic_menu_trash_holo_dark);
delete.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
@@ -149,6 +154,9 @@ public class ProfileConfig extends SettingsPreferenceFragment
case MENU_NFC_WRITE:
startNFCProfileWriter();
return true;
+ case MENU_WIFI:
+ startWifiTrigger();
+ return true;
default:
return false;
}
@@ -178,6 +186,16 @@ public class ProfileConfig extends SettingsPreferenceFragment
pa.startActivity(i);
}
+ private void startWifiTrigger() {
+ final PreferenceActivity pa = (PreferenceActivity) getActivity();
+ final String title = getResources().getString(R.string.profile_trigger_title_wifi,
+ mProfile.getName());
+ final Bundle args = new Bundle();
+ args.putParcelable("profile", mProfile);
+
+ pa.startPreferencePanel(WifiTriggerFragment.class.getName(), args, 0, title, null, 0);
+ }
+
private void fillList() {
PreferenceScreen prefSet = getPreferenceScreen();
diff --git a/src/com/android/settings/profiles/WifiTriggerAPPreference.java b/src/com/android/settings/profiles/WifiTriggerAPPreference.java
new file mode 100644
index 0000000..c886bc1
--- /dev/null
+++ b/src/com/android/settings/profiles/WifiTriggerAPPreference.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2013 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.profiles;
+
+import android.app.Profile;
+import android.content.Context;
+import android.net.wifi.WifiConfiguration;
+import android.os.Bundle;
+import android.preference.Preference;
+
+import com.android.settings.R;
+
+public class WifiTriggerAPPreference extends Preference {
+
+ private String mSsid;
+ private int mTriggerType = Profile.TriggerState.DISABLED;
+ private WifiConfiguration mConfig;
+
+ WifiTriggerAPPreference(Context context, WifiConfiguration config) {
+ super(context);
+ setWidgetLayoutResource(R.layout.preference_widget_wifi_signal);
+ loadConfig(config);
+ setTitle(mSsid);
+ }
+
+ public void setTriggerType(int trigger) {
+ mTriggerType = trigger;
+ }
+
+ public int getTriggerType() {
+ return mTriggerType;
+ }
+
+ private void loadConfig(WifiConfiguration config) {
+ mSsid = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID));
+ mConfig = config;
+ }
+
+ public WifiConfiguration getConfig() {
+ return mConfig;
+ }
+
+ public String getSSID() {
+ return mSsid;
+ }
+
+ public static String removeDoubleQuotes(String string) {
+ final int length = string.length();
+ if (length >= 2) {
+ if (string.startsWith("\"") && string.endsWith("\"")) {
+ return string.substring(1, length - 1);
+ }
+ }
+ return string;
+ }
+}
diff --git a/src/com/android/settings/profiles/WifiTriggerFragment.java b/src/com/android/settings/profiles/WifiTriggerFragment.java
new file mode 100644
index 0000000..73d40a2
--- /dev/null
+++ b/src/com/android/settings/profiles/WifiTriggerFragment.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright (C) 2013 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.profiles;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.app.Profile;
+import android.app.ProfileManager;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.res.Resources;
+import android.net.wifi.WifiConfiguration;
+import android.net.wifi.WifiManager;
+import android.os.Bundle;
+import android.preference.Preference;
+import android.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.SettingsPreferenceFragment;
+
+/**
+ * Settings Preference to configure triggers to switch profiles base on Wi-Fi events
+ */
+public class WifiTriggerFragment extends SettingsPreferenceFragment {
+ private Profile mProfile;
+ private WifiTriggerAPPreference mSelectedAccessPoint;
+ private ProfileManager mProfileManager;
+ private WifiManager mWifiManager;
+
+ private static final int WIFI_TRIGGER = 1;
+
+ @Override
+ public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+ mProfileManager = (ProfileManager) getSystemService(Context.PROFILE_SERVICE);
+ mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
+ addPreferencesFromResource(R.xml.wifi_settings);
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ loadWifiConfiguration();
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ Bundle args = getArguments();
+ if (args != null) {
+ mProfile = args.getParcelable("profile");
+ }
+ }
+
+ private void loadWifiConfiguration() {
+ final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
+ final Resources res = getResources();
+ final List<WifiTriggerAPPreference> aps = new ArrayList<WifiTriggerAPPreference>();
+
+ getPreferenceScreen().removeAll();
+
+ if (configs != null) {
+ for (WifiConfiguration config : configs) {
+ WifiTriggerAPPreference accessPoint = new WifiTriggerAPPreference(getActivity(), config);
+ int state = mProfile.getWifiTrigger(accessPoint.getSSID());
+ String summary = res.getStringArray(R.array.profile_trigger_wifi_options)[state];
+
+ accessPoint.setSummary(summary);
+ accessPoint.setTriggerType(state);
+ aps.add(accessPoint);
+ }
+ }
+
+ Collections.sort(aps, new Comparator<WifiTriggerAPPreference>() {
+ @Override
+ public int compare(WifiTriggerAPPreference o1, WifiTriggerAPPreference o2) {
+ if (o1.getTriggerType() == o2.getTriggerType()) {
+ return o1.getSSID().compareTo(o2.getSSID());
+ }
+ return o1.getTriggerType() < o2.getTriggerType() ? -1 : 1;
+ }
+ });
+ for (WifiTriggerAPPreference ap: aps) {
+ getPreferenceScreen().addPreference(ap);
+ }
+ }
+
+ @Override
+ public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
+ if (preference instanceof WifiTriggerAPPreference) {
+ mSelectedAccessPoint = (WifiTriggerAPPreference) preference;
+ showDialog(WIFI_TRIGGER);
+ return true;
+ }
+ return super.onPreferenceTreeClick(screen, preference);
+ }
+
+ @Override
+ public Dialog onCreateDialog(int dialogId) {
+ switch (dialogId) {
+ case WIFI_TRIGGER:
+ final String ssid = mSelectedAccessPoint.getSSID();
+ int currentTriggerType = mProfile.getWifiTrigger(ssid);
+
+ return new AlertDialog.Builder(getActivity())
+ .setTitle(R.string.profile_trigger_configure)
+ .setSingleChoiceItems(R.array.profile_trigger_wifi_options, currentTriggerType,
+ new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ mProfile.setWifiTrigger(ssid, which);
+ mProfileManager.updateProfile(mProfile);
+ loadWifiConfiguration();
+ dialog.dismiss();
+ }
+ })
+ .create();
+ }
+ return super.onCreateDialog(dialogId);
+ }
+}