diff options
author | Roman Birg <roman@cyngn.com> | 2014-09-17 10:37:05 -0700 |
---|---|---|
committer | Adnan Begovic <adnan@cyngn.com> | 2015-10-26 16:11:13 -0700 |
commit | 6455208e1334114881be837cfbc26d3ae06f9ce3 (patch) | |
tree | 219d326bff3a7a8d451f10f08fc31fe8b27fbe44 /src/com/android/settings/profiles | |
parent | 0f8cf2c7f9ebf6ce40f905a3090b47ad6a87d0bc (diff) | |
download | packages_apps_Settings-6455208e1334114881be837cfbc26d3ae06f9ce3.zip packages_apps_Settings-6455208e1334114881be837cfbc26d3ae06f9ce3.tar.gz packages_apps_Settings-6455208e1334114881be837cfbc26d3ae06f9ce3.tar.bz2 |
Settings: squash of new profile UI for cm-12.0
Additions for cm-12.0:
- removed expanded desktop
- added add profile FAB
- updated tabs for meterial style
- added enabler switch bar
Squashed commits:
-------------------------------
Settings: refactor Profiles to be more user friendly
* Remove the notion of Profile app groups
* Rearrange profile setup flow, now we first present the user with ways
which will trigger the profile, then allow them to set what happens when
the profile is triggered.
Change-Id: I2870df894046cd43c21c67991fb5efc9c3fec837
Signed-off-by: Roman Birg <roman@cyngn.com>
Settings: cleanup new profiles UI
* cleanup old code
* for configuring profiles, add options to modify triggers and remove
the profile
* remove separate activity
* don't hardcode padding pixel sizes
Change-Id: I6b1d05c9f918714249411006c7562496e2a7539e
Signed-off-by: Roman Birg <roman@cyngn.com>
Profiles: fix expanded desktop settings not applying
Change-Id: Ia523145bf47bfc189c29b035ad5ec6e053cf9296
Settings: Fix profiles volume override summary.
Change-Id: I499cfb3006a7fc7b5e931ac854a7437f5d141523
Settings: fix Profiles expanded desktop choice mapping
Change-Id: Ib96811a0c268ca68a7b9732283c5abc142d143a4
Signed-off-by: Roman Birg <roman@cyngn.com>
Settings: add "Untouched" ring mode option in profiles
Change-Id: I4ac2d63119b328533217104a799cb0660ecddfb9
Signed-off-by: Roman Birg <roman@cyngn.com>
Settings: profiles: fix volume streams override behavior
Before we would always assume to override the stream, but that is not
always the case. Add a checkbox to turn the override on and off.
Signed-off-by: Roman Birg <roman@cyngn.com>
Change-Id: Ic1a58d59a9e514d4c2ccf376191ac7989bbead26
Diffstat (limited to 'src/com/android/settings/profiles')
36 files changed, 2489 insertions, 2647 deletions
diff --git a/src/com/android/settings/profiles/AppGroupConfig.java b/src/com/android/settings/profiles/AppGroupConfig.java deleted file mode 100644 index 8fbcfd0..0000000 --- a/src/com/android/settings/profiles/AppGroupConfig.java +++ /dev/null @@ -1,438 +0,0 @@ -/* - * 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.profiles; - -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; - -import android.app.AlertDialog; -import android.app.Dialog; -import android.app.NotificationGroup; -import android.app.ProfileManager; -import android.content.Context; -import android.content.DialogInterface; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.os.Handler; -import android.preference.Preference; -import android.preference.PreferenceGroup; -import android.preference.PreferenceScreen; -import android.util.Log; -import android.view.ContextMenu; -import android.view.ContextMenu.ContextMenuInfo; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.AdapterView.AdapterContextMenuInfo; -import android.widget.AdapterView.OnItemClickListener; -import android.widget.BaseAdapter; -import android.widget.ImageView; -import android.widget.ListView; -import android.widget.TextView; -import android.widget.Toast; - -import com.android.settings.R; -import com.android.settings.SettingsPreferenceFragment; - -public class AppGroupConfig extends SettingsPreferenceFragment - implements Preference.OnPreferenceChangeListener { - - private static String TAG = "AppGroupConfig"; - - private static final int DIALOG_APPS = 0; - - private static final int DELETE_CONFIRM = 1; - - private static final int DELETE_GROUP_CONFIRM = 2; - - public static final String PROFILE_SERVICE = "profile"; - - private ListView mListView; - - private PackageManager mPackageManager; - - private NotificationGroup mNotificationGroup; - - private ProfileManager mProfileManager; - - private NamePreference mNamePreference; - - private static final int MENU_DELETE = Menu.FIRST; - - private static final int MENU_ADD = Menu.FIRST + 1; - - PackageAdaptor mAppAdapter; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - mProfileManager = (ProfileManager) getActivity().getSystemService(PROFILE_SERVICE); - addPreferencesFromResource(R.xml.application_list); - - final Bundle args = getArguments(); - if (args != null) { - mNotificationGroup = (NotificationGroup) args.getParcelable("NotificationGroup"); - mPackageManager = getPackageManager(); - mAppAdapter = new PackageAdaptor(mPackageManager.getInstalledPackages(0)); - mAppAdapter.update(); - - updatePackages(); - - setHasOptionsMenu(true); - } - } - - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - MenuItem delete = menu.add(0, MENU_DELETE, 0, R.string.profile_menu_delete) - .setIcon(R.drawable.ic_menu_trash_holo_dark); - delete.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | - MenuItem.SHOW_AS_ACTION_WITH_TEXT); - - MenuItem addApplication = menu.add(0, MENU_ADD, 0, R.string.profiles_add) - .setIcon(R.drawable.ic_menu_add) - .setAlphabeticShortcut('a'); - addApplication.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | - MenuItem.SHOW_AS_ACTION_WITH_TEXT); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case MENU_DELETE: - deleteNotificationGroup(); - return true; - case MENU_ADD: - addNewApp(); - return true; - default: - return false; - } - } - - Preference mAddPreference; - - Preference mDeletePreference; - - private void updatePackages() { - PreferenceScreen prefSet = getPreferenceScreen(); - - // Add the General section - PreferenceGroup generalPrefs = (PreferenceGroup) prefSet.findPreference("general_section"); - if (generalPrefs != null) { - generalPrefs.removeAll(); - - // Name preference - mNamePreference = new NamePreference(getActivity(), mNotificationGroup.getName()); - mNamePreference.setOnPreferenceChangeListener(this); - generalPrefs.addPreference(mNamePreference); - } - - PreferenceGroup applicationsList = (PreferenceGroup) prefSet.findPreference("applications_list"); - if (applicationsList != null) { - applicationsList.removeAll(); - for (String pkg : mNotificationGroup.getPackages()) { - Preference pref = new Preference(getActivity()); - try { - PackageInfo group = mPackageManager.getPackageInfo(pkg, 0); - pref.setKey(group.packageName); - pref.setTitle(group.applicationInfo.loadLabel(mPackageManager)); - Drawable icon = group.applicationInfo.loadIcon(mPackageManager); - pref.setIcon(icon); - pref.setSelectable(true); - pref.setPersistent(false); - applicationsList.addPreference(pref); - } catch (NameNotFoundException e) { - e.printStackTrace(); - } - } - } - } - - @Override - public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { - menu.add(0, R.string.profile_menu_delete, 0, R.string.profile_menu_delete); - } - - @Override - public boolean onContextItemSelected(MenuItem item) { - AdapterContextMenuInfo aMenuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); - PackageItem selectedGroup = (PackageItem) mListView.getItemAtPosition(aMenuInfo.position); - switch (item.getItemId()) { - case R.string.profile_menu_delete: - deleteAppFromGroup(selectedGroup); - return true; - } - return super.onOptionsItemSelected(item); - } - - private void deleteAppFromGroup(PackageItem selectedGroup) { - if (selectedGroup != null) { - mNotificationGroup.removePackage(selectedGroup.packageName); - updatePackages(); - } - } - - @Override - public void onPause() { - if (mNotificationGroup != null) { - mProfileManager.addNotificationGroup(mNotificationGroup); - } - super.onPause(); - } - - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if (preference == mNamePreference) { - String name = mNamePreference.getName().toString(); - if (!name.equals(mNotificationGroup.getName())) { - if (!mProfileManager.notificationGroupExists(name)) { - mNotificationGroup.setName(name); - } else { - mNamePreference.setName(mNotificationGroup.getName()); - Toast.makeText(getActivity(), R.string.duplicate_appgroup_name, Toast.LENGTH_LONG).show(); - } - } - } - return true; - } - - @Override - public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { - if (preference instanceof Preference) { - String deleteItem = preference.getKey(); - removeApp(deleteItem); - return true; - } - return super.onPreferenceTreeClick(preferenceScreen, preference); - } - - private void addNewApp() { - showDialog(DIALOG_APPS); - // TODO: switch to using the built in app list rather than dialog box? - } - - private void removeApp(String key) { - mPackageToDelete = key.toString(); - showDialog(DELETE_CONFIRM); - } - - private void deleteNotificationGroup() { - showDialog(DELETE_GROUP_CONFIRM); - } - - @Override - public Dialog onCreateDialog(int id) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - final Dialog dialog; - switch (id) { - case DIALOG_APPS: - final ListView list = new ListView(getActivity()); - list.setAdapter(mAppAdapter); - builder.setTitle(R.string.profile_choose_app); - builder.setView(list); - dialog = builder.create(); - list.setOnItemClickListener(new OnItemClickListener() { - @Override - public void onItemClick(AdapterView<?> parent, View view, int position, long id) { - PackageItem info = (PackageItem) parent.getItemAtPosition(position); - mNotificationGroup.addPackage(info.packageName); - updatePackages(); - dialog.cancel(); - } - }); - break; - case DELETE_CONFIRM: - builder.setMessage(R.string.profile_app_delete_confirm); - builder.setTitle(R.string.profile_menu_delete); - builder.setIconAttribute(android.R.attr.alertDialogIcon); - builder.setPositiveButton(android.R.string.yes, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - doDelete(); - } - }); - builder.setNegativeButton(android.R.string.no, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - } - }); - dialog = builder.create(); - break; - case DELETE_GROUP_CONFIRM: - builder.setMessage(R.string.profile_delete_appgroup); - builder.setTitle(R.string.profile_menu_delete); - builder.setIconAttribute(android.R.attr.alertDialogIcon); - builder.setPositiveButton(android.R.string.yes, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - mProfileManager.removeNotificationGroup(mNotificationGroup); - mNotificationGroup = null; - finish(); - } - }); - builder.setNegativeButton(android.R.string.no, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - } - }); - dialog = builder.create(); - break; - default: - dialog = null; - } - return dialog; - } - - String mPackageToDelete; - - private void doDelete() { - mNotificationGroup.removePackage(mPackageToDelete); - updatePackages(); - } - - class PackageItem implements Comparable<PackageItem> { - String title; - String packageName; - Drawable icon; - boolean enabled; - - @Override - public int compareTo(PackageItem another) { - if (enabled != another.enabled) { - return enabled ? -1 : 1; - } - int titleResult = title.compareToIgnoreCase(another.title); - if (titleResult != 0) { - return titleResult; - } - return packageName.compareTo(another.packageName); - } - } - - class PackageAdaptor extends BaseAdapter { - - protected List<PackageInfo> mInstalledPackageInfo; - - protected List<PackageItem> mInstalledPackages = new LinkedList<PackageItem>(); - - private void reloadList() { - final Handler handler = new Handler(); - new Thread(new Runnable() { - - @Override - public void run() { - synchronized (mInstalledPackages) { - mInstalledPackages.clear(); - for (PackageInfo info : mInstalledPackageInfo) { - final PackageItem item = new PackageItem(); - ApplicationInfo applicationInfo = info.applicationInfo; - item.title = applicationInfo.loadLabel(mPackageManager).toString(); - item.icon = applicationInfo.loadIcon(mPackageManager); - item.packageName = applicationInfo.packageName; - item.enabled = applicationInfo.enabled; - handler.post(new Runnable() { - @Override - public void run() { - int index = Collections.binarySearch(mInstalledPackages, item); - if (index < 0) { - index = -index - 1; - mInstalledPackages.add(index, item); - } - notifyDataSetChanged(); - } - }); - } - } - } - }).start(); - } - - public PackageAdaptor(List<PackageInfo> installedPackagesInfo) { - mInstalledPackageInfo = installedPackagesInfo; - } - - public void update() { - reloadList(); - } - - @Override - public int getCount() { - return mInstalledPackages.size(); - } - - @Override - public PackageItem getItem(int position) { - return mInstalledPackages.get(position); - } - - @Override - public long getItemId(int position) { - return mInstalledPackages.get(position).packageName.hashCode(); - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - ViewHolder holder; - if (convertView != null) { - holder = (ViewHolder) convertView.getTag(); - } else { - final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); - convertView = layoutInflater.inflate(R.layout.preference_icon, null, false); - holder = new ViewHolder(); - convertView.setTag(holder); - holder.title = (TextView) convertView.findViewById(com.android.internal.R.id.title); - holder.summary = (TextView) convertView - .findViewById(com.android.internal.R.id.summary); - holder.icon = (ImageView) convertView.findViewById(R.id.icon); - } - PackageItem applicationInfo = getItem(position); - - if (holder.title != null) { - holder.title.setText(applicationInfo.title); - } - if (holder.summary != null) { - holder.summary.setVisibility(View.GONE); - } - if (holder.icon != null) { - Drawable loadIcon = applicationInfo.icon; - holder.icon.setImageDrawable(loadIcon); - } - return convertView; - } - } - - static class ViewHolder { - TextView title; - TextView summary; - ImageView icon; - } -} diff --git a/src/com/android/settings/profiles/AppGroupList.java b/src/com/android/settings/profiles/AppGroupList.java deleted file mode 100644 index 088b344..0000000 --- a/src/com/android/settings/profiles/AppGroupList.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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.profiles; - -import java.util.UUID; - -import android.app.NotificationGroup; -import android.app.ProfileManager; -import android.os.Bundle; -import android.preference.Preference; -import android.preference.PreferenceActivity; -import android.preference.PreferenceScreen; -import android.provider.Settings; - -import com.android.settings.R; -import com.android.settings.SettingsActivity; -import com.android.settings.SettingsPreferenceFragment; -import com.android.settings.Utils; - -public class AppGroupList extends SettingsPreferenceFragment { - - private static final String TAG = "AppGroupSettings"; - public static final String PROFILE_SERVICE = "profile"; - - private ProfileManager mProfileManager; - - // constant value that can be used to check return code from sub activity. - private static final int APP_GROUP_CONFIG = 1; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - addPreferencesFromResource(R.xml.appgroup_list); - mProfileManager = (ProfileManager) getActivity().getSystemService(PROFILE_SERVICE); - } - - @Override - public void onResume() { - super.onResume(); - refreshList(); - - // On tablet devices remove the padding - if (Utils.isTablet(getActivity())) { - getListView().setPadding(0, 0, 0, 0); - } - } - - public void refreshList() { - PreferenceScreen appgroupList = getPreferenceScreen(); - appgroupList.removeAll(); - - // Add the existing app groups - for (NotificationGroup group : mProfileManager.getNotificationGroups()) { - PreferenceScreen pref = new PreferenceScreen(getActivity(), null); - pref.setKey(group.getUuid().toString()); - pref.setTitle(group.getName()); - pref.setPersistent(false); - appgroupList.addPreference(pref); - } - } - - @Override - public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { - if (preference instanceof PreferenceScreen) { - NotificationGroup group = mProfileManager.getNotificationGroup( - UUID.fromString(preference.getKey())); - editGroup(group); - } - return super.onPreferenceTreeClick(preferenceScreen, preference); - } - - private void editGroup(NotificationGroup group) { - Bundle args = new Bundle(); - args.putParcelable("NotificationGroup", group); - - SettingsActivity pa = (SettingsActivity) getActivity(); - pa.startPreferencePanel(AppGroupConfig.class.getName(), args, - R.string.profile_appgroup_manage, null, this, APP_GROUP_CONFIG); - } -} diff --git a/src/com/android/settings/profiles/ApplicationItemPreference.java b/src/com/android/settings/profiles/ApplicationItemPreference.java deleted file mode 100644 index 1247e85..0000000 --- a/src/com/android/settings/profiles/ApplicationItemPreference.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.profiles; - -import android.content.Context; -import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; -import android.preference.Preference; -import android.util.AttributeSet; -import android.util.Log; -import android.view.View; -import android.widget.ImageView; -import com.android.settings.R; - -public class ApplicationItemPreference extends Preference { - - private static String TAG = "ApplicationItemPreference"; - - private Drawable mIcon; - - public ApplicationItemPreference(Context context) { - this(context, null, 0); - } - - public ApplicationItemPreference(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - setLayoutResource(R.layout.preference_icon); - TypedArray a = context.obtainStyledAttributes(attrs, - R.styleable.IconPreferenceScreen, defStyle, 0); - mIcon = a.getDrawable(R.styleable.IconPreferenceScreen_icon); - } - - public void setIcon(Drawable icon){ - mIcon = icon; - } - - @Override - public void onBindView(View view) { - super.onBindView(view); - - float valueDips = 36.0f; - final float scale = getContext().getResources().getDisplayMetrics().density; - int valuePixels = (int) (valueDips * scale + 0.5f); - - ImageView imageView = (ImageView) view.findViewById(R.id.icon); - if (imageView != null && mIcon != null) { - imageView.setAdjustViewBounds(true); - imageView.setMaxHeight(valuePixels); - imageView.setMaxWidth(valuePixels); - imageView.setImageDrawable(mIcon); - } - } -} diff --git a/src/com/android/settings/profiles/BluetoothTriggerPreference.java b/src/com/android/settings/profiles/BluetoothTriggerPreference.java deleted file mode 100644 index 848fc28..0000000 --- a/src/com/android/settings/profiles/BluetoothTriggerPreference.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.bluetooth.BluetoothDevice; -import android.content.Context; - -public class BluetoothTriggerPreference extends AbstractTriggerPreference { - - private String mAddress; - - BluetoothTriggerPreference(Context context, BluetoothDevice device) { - super(context); - mAddress = device.getAddress(); - if (device.getAlias() != null) { - setTitle(device.getAlias()); - } else { - setTitle(device.getName()); - } - } - - BluetoothTriggerPreference(Context context, String name, String address) { - super(context); - mAddress = address; - setTitle(name); - } - - public String getAddress() { - return mAddress; - } -} diff --git a/src/com/android/settings/profiles/NFCProfileTagCallback.java b/src/com/android/settings/profiles/NFCProfileTagCallback.java new file mode 100644 index 0000000..e3fd5ef --- /dev/null +++ b/src/com/android/settings/profiles/NFCProfileTagCallback.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2014 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.nfc.Tag; + +public interface NFCProfileTagCallback { + public void onTagRead(Tag tag); +} diff --git a/src/com/android/settings/profiles/NFCProfileUtils.java b/src/com/android/settings/profiles/NFCProfileUtils.java index f6461d1..4c97c8d 100644 --- a/src/com/android/settings/profiles/NFCProfileUtils.java +++ b/src/com/android/settings/profiles/NFCProfileUtils.java @@ -37,7 +37,7 @@ public class NFCProfileUtils { 0, 100, 10000 }; - static void vibrate(Context context) { + public static void vibrate(Context context) { Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(VIBRATION_PATTERN, -1); } @@ -45,7 +45,7 @@ public class NFCProfileUtils { /* * Writes an NdefMessage to a NFC tag */ - static boolean writeTag(NdefMessage message, Tag tag) { + public static boolean writeTag(NdefMessage message, Tag tag) { int size = message.toByteArray().length; try { Ndef ndef = Ndef.get(tag); @@ -120,7 +120,7 @@ public class NFCProfileUtils { * Convert a profiles into an NdefMessage. The profile UUID is 16 bytes and * stored with the cm/profile mimetype */ - static NdefMessage getProfileAsNdef(Profile profile) { + public static NdefMessage getProfileAsNdef(Profile profile) { byte[] profileBytes = NFCProfileUtils.asByteArray(profile.getUuid()); NdefRecord record = NdefRecord.createMime(NFCProfile.PROFILE_MIME_TYPE, profileBytes); diff --git a/src/com/android/settings/profiles/NamePreference.java b/src/com/android/settings/profiles/NamePreference.java deleted file mode 100644 index 7b48e0c..0000000 --- a/src/com/android/settings/profiles/NamePreference.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * 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.profiles; - -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.preference.Preference; -import android.view.View; -import android.widget.EditText; -import android.widget.LinearLayout; -import android.widget.TextView; - -import com.android.settings.R; - -public class NamePreference extends Preference implements - View.OnClickListener, Preference.OnPreferenceChangeListener { - private static final String TAG = NamePreference.class.getSimpleName(); - - private TextView mNameView; - - private String mName; - - /** - * @param context - * @param title - */ - public NamePreference(Context context, String name) { - super(context); - mName = name.toString(); - init(); - } - - /** - * @param context - */ - public NamePreference(Context context) { - super(context); - init(); - } - - @Override - public void onBindView(View view) { - super.onBindView(view); - - View namePref = view.findViewById(R.id.name_pref); - if ((namePref != null) && namePref instanceof LinearLayout) { - namePref.setOnClickListener(this); - } - - mNameView = (TextView) view.findViewById(R.id.title); - - updatePreferenceViews(); - } - - private void init() { - setLayoutResource(R.layout.preference_name); - } - - public void setName(String name) { - mName = (name.toString()); - updatePreferenceViews(); - } - - public String getName() { - return(mName.toString()); - } - - private void updatePreferenceViews() { - if (mNameView != null) { - mNameView.setText(mName.toString()); - } - } - - @Override - public void onClick(android.view.View v) { - if (v != null) { - Context context = getContext(); - if (context != null) { - final EditText entry = new EditText(context); - entry.setSingleLine(); - entry.setText(mName.toString()); - - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(R.string.rename_dialog_title); - builder.setMessage(R.string.rename_dialog_message); - builder.setView(entry, 34, 16, 34, 16); - builder.setPositiveButton(android.R.string.ok, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - String value = entry.getText().toString(); - mName = value.toString(); - mNameView.setText(value.toString()); - callChangeListener(this); - } - }); - builder.setNegativeButton(android.R.string.cancel, null); - AlertDialog dialog = builder.create(); - dialog.show(); - ((TextView)dialog.findViewById(android.R.id.message)).setTextAppearance(context, - android.R.style.TextAppearance_DeviceDefault_Small); - } - } - } - - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - callChangeListener(preference); - return false; - } -} diff --git a/src/com/android/settings/profiles/ProfileAirplaneModePreference.java b/src/com/android/settings/profiles/ProfileAirplaneModePreference.java deleted file mode 100644 index 067e43b..0000000 --- a/src/com/android/settings/profiles/ProfileAirplaneModePreference.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * 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.profiles; - -import android.app.AlertDialog; -import android.app.Dialog; -import android.content.Context; -import android.content.DialogInterface; -import android.preference.Preference; -import android.util.AttributeSet; -import android.view.View; -import android.view.ViewGroup; -import android.widget.CheckBox; -import android.widget.CompoundButton; -import android.widget.LinearLayout; - -import com.android.settings.R; - -public class ProfileAirplaneModePreference extends Preference implements - CompoundButton.OnCheckedChangeListener, View.OnClickListener { - - private boolean mProtectFromCheckedChange = false; - - private CheckBox mCheckBox; - - final static String TAG = "ProfileSilentModePreference"; - - private ProfileConfig.AirplaneModeItem mAirplaneModeItem; - - final static int defaultChoice = -1; - - private int currentChoice; - - /** - * @param context - * @param attrs - * @param defStyle - */ - public ProfileAirplaneModePreference(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - init(); - } - - /** - * @param context - * @param attrs - */ - public ProfileAirplaneModePreference(Context context, AttributeSet attrs) { - super(context, attrs); - init(); - } - - /** - * @param context - */ - public ProfileAirplaneModePreference(Context context) { - super(context); - init(); - } - - @Override - public View getView(View convertView, ViewGroup parent) { - View view = super.getView(convertView, parent); - - View widget = view.findViewById(R.id.profile_checkbox); - if ((widget != null) && widget instanceof CheckBox) { - mCheckBox = (CheckBox) widget; - mCheckBox.setOnCheckedChangeListener(this); - - mProtectFromCheckedChange = true; - mCheckBox.setChecked(isChecked()); - mProtectFromCheckedChange = false; - } - - View textLayout = view.findViewById(R.id.text_layout); - if ((textLayout != null) && textLayout instanceof LinearLayout) { - textLayout.setOnClickListener(this); - } - - return view; - } - - private void init() { - setLayoutResource(R.layout.preference_streamvolume); - } - - public boolean isChecked() { - return mAirplaneModeItem != null && mAirplaneModeItem.mSettings.isOverride(); - } - - public void setAirplaneModeItem(ProfileConfig.AirplaneModeItem airplaneModeItem) { - mAirplaneModeItem = airplaneModeItem; - - if (mCheckBox != null) { - mCheckBox.setChecked(mAirplaneModeItem.mSettings.isOverride()); - } - } - - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (mProtectFromCheckedChange) { - return; - } - - mAirplaneModeItem.mSettings.setOverride(isChecked); - - callChangeListener(isChecked); - } - - protected Dialog createAirplaneModeDialog() { - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - final String[] AirplaneModeValues = getContext().getResources().getStringArray( - R.array.profile_connection_values); - - currentChoice = mAirplaneModeItem.mSettings.getValue(); - - builder.setTitle(R.string.profile_airplanemode_title); - builder.setSingleChoiceItems(R.array.profile_connection_entries, currentChoice, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int item) { - currentChoice = item; - } - }); - - builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int item) { - if (currentChoice != defaultChoice) { - int value = Integer.parseInt(AirplaneModeValues[currentChoice]); - mAirplaneModeItem.mSettings.setValue(currentChoice); - setSummary(value == 1 ? getContext().getString(R.string.connection_state_enabled) : getContext() - .getString(R.string.connection_state_disabled)); - } - } - }); - - builder.setNegativeButton(android.R.string.cancel, null); - return builder.create(); - } - - public ProfileConfig.AirplaneModeItem getAirplaneModeItem() { - return mAirplaneModeItem; - } - - @Override - public void onClick(android.view.View v) { - if ((v != null) && (R.id.text_layout == v.getId())) { - createAirplaneModeDialog().show(); - } - } - - public void setSummary(Context context) { - int value = mAirplaneModeItem.mSettings.getValue(); - setSummary(value == 1 ? getContext().getString(R.string.connection_state_enabled) : getContext().getString( - R.string.connection_state_disabled)); - } -} diff --git a/src/com/android/settings/profiles/ProfileConfig.java b/src/com/android/settings/profiles/ProfileConfig.java deleted file mode 100644 index 3532720..0000000 --- a/src/com/android/settings/profiles/ProfileConfig.java +++ /dev/null @@ -1,479 +0,0 @@ -/* - * 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.profiles; - -import java.util.ArrayList; -import java.util.UUID; - -import android.app.AirplaneModeSettings; -import android.app.AlertDialog; -import android.app.ConnectionSettings; -import android.app.Profile; -import android.app.ProfileGroup; -import android.app.ProfileManager; -import android.app.RingModeSettings; -import android.app.StreamSettings; -import android.app.admin.DevicePolicyManager; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.media.AudioManager; -import android.net.wimax.WimaxHelper; -import android.os.Bundle; -import android.preference.ListPreference; -import android.preference.Preference; -import android.preference.PreferenceActivity; -import android.preference.PreferenceGroup; -import android.preference.PreferenceScreen; -import android.telephony.TelephonyManager; -import android.util.Log; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.widget.Toast; - -import com.android.settings.R; -import com.android.settings.SettingsActivity; -import static com.android.settings.profiles.ProfilesUtils.*; -import com.android.settings.SettingsPreferenceFragment; - -public class ProfileConfig extends SettingsPreferenceFragment - implements Preference.OnPreferenceChangeListener { - - static final String TAG = "ProfileConfig"; - - public static final String PROFILE_SERVICE = "profile"; - - private ProfileManager mProfileManager; - - private static final int MENU_NFC_WRITE = Menu.FIRST; - - private static final int MENU_DELETE = Menu.FIRST + 1; - - private static final int MENU_TRIGGERS = Menu.FIRST + 2; - - private Profile mProfile; - - private NamePreference mNamePreference; - - private ListPreference mScreenLockModePreference; - - // constant value that can be used to check return code from sub activity. - private static final int PROFILE_GROUP_DETAILS = 1; - - private StreamItem[] mStreams; - - private ArrayList<ConnectionItem> mConnections; - - private RingModeItem mRingMode; - - private AirplaneModeItem mAirplaneMode; - - @Override - public void onCreate(Bundle icicle) { - super.onCreate(icicle); - - mStreams = new StreamItem[] { - new StreamItem(AudioManager.STREAM_ALARM, getString(R.string.alarm_volume_title)), - new StreamItem(AudioManager.STREAM_MUSIC, getString(R.string.media_volume_title)), - new StreamItem(AudioManager.STREAM_RING, getString(R.string.incoming_call_volume_title)), - new StreamItem(AudioManager.STREAM_NOTIFICATION, getString(R.string.notification_volume_title)) - }; - - mConnections = new ArrayList<ConnectionItem>(); - if (deviceSupportsBluetooth()) { - mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_BLUETOOTH, getString(R.string.toggleBluetooth))); - } - mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_GPS, getString(R.string.toggleGPS))); - mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_WIFI, getString(R.string.toggleWifi))); - mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_SYNC, getString(R.string.toggleSync))); - if (deviceSupportsMobileData(getActivity())) { - mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_MOBILEDATA, getString(R.string.toggleData))); - mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_WIFIAP, getString(R.string.toggleWifiAp))); - final TelephonyManager tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE); - if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) { - mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_2G3G, getString(R.string.toggle2g3g), R.array.profile_networkmode_entries)); - } - } - if (WimaxHelper.isWimaxSupported(getActivity())) { - mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_WIMAX, getString(R.string.toggleWimax))); - } - if (deviceSupportsNfc(getActivity())) { - mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_NFC, getString(R.string.toggleNfc))); - } - - addPreferencesFromResource(R.xml.profile_config); - - mProfileManager = (ProfileManager) getActivity().getSystemService(PROFILE_SERVICE); - - final Bundle args = getArguments(); - mProfile = (args != null) ? (Profile) args.getParcelable("Profile") : null; - - if (mProfile == null) { - mProfile = new Profile(getString(R.string.new_profile_name)); - mProfileManager.addProfile(mProfile); - } - - setHasOptionsMenu(true); - - } - - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - 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 triggers = menu.add(0, MENU_TRIGGERS, 0, R.string.profile_triggers) - .setIcon(R.drawable.ic_location); - triggers.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 | - MenuItem.SHOW_AS_ACTION_WITH_TEXT); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case MENU_DELETE: - deleteProfile(); - return true; - case MENU_NFC_WRITE: - startNFCProfileWriter(); - return true; - case MENU_TRIGGERS: - startTriggerFragment(); - return true; - default: - return false; - } - } - - @Override - public void onResume() { - super.onResume(); - mProfile = mProfileManager.getProfile(mProfile.getUuid()); - fillList(); - } - - @Override - public void onPause() { - super.onPause(); - // Save profile here - if (mProfile != null) { - mProfileManager.updateProfile(mProfile); - } - } - - private void startNFCProfileWriter() { - SettingsActivity pa = (SettingsActivity) getActivity(); - Intent i = new Intent(this.getActivity(), NFCProfileWriter.class); - i.putExtra(NFCProfileWriter.EXTRA_PROFILE_UUID, mProfile.getUuid().toString()); - i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); - pa.startActivity(i); - } - - private void startTriggerFragment() { - final SettingsActivity pa = (SettingsActivity) getActivity(); - final Bundle args = new Bundle(); - args.putParcelable("profile", mProfile); - - pa.startPreferencePanel(TriggersFragment.class.getName(), args, 0, "", null, 0); - } - - private void fillList() { - PreferenceScreen prefSet = getPreferenceScreen(); - - // Add the General section - PreferenceGroup generalPrefs = (PreferenceGroup) prefSet.findPreference("profile_general_section"); - if (generalPrefs != null) { - generalPrefs.removeAll(); - - // Name preference - mNamePreference = new NamePreference(getActivity(), mProfile.getName()); - mNamePreference.setOnPreferenceChangeListener(this); - generalPrefs.addPreference(mNamePreference); - } - - // Populate system settings - PreferenceGroup systemPrefs = (PreferenceGroup) prefSet.findPreference("profile_system_settings"); - if (systemPrefs != null) { - systemPrefs.removeAll(); - // Ring mode preference - if (mRingMode == null) { - mRingMode = new RingModeItem(); - } - RingModeSettings rms = mProfile.getRingMode(); - if (rms == null) { - rms = new RingModeSettings(); - mProfile.setRingMode(rms); - } - mRingMode.mSettings = rms; - ProfileRingModePreference rmp = new ProfileRingModePreference(getActivity()); - rmp.setRingModeItem(mRingMode); - rmp.setTitle(R.string.ring_mode_title); - rmp.setPersistent(false); - rmp.setSummary(getActivity()); - rmp.setOnPreferenceChangeListener(this); - mRingMode.mCheckbox = rmp; - systemPrefs.addPreference(rmp); - - // Airplane mode preference - if (mAirplaneMode == null) { - mAirplaneMode = new AirplaneModeItem(); - } - AirplaneModeSettings ams = mProfile.getAirplaneMode(); - if (ams == null) { - ams = new AirplaneModeSettings(); - mProfile.setAirplaneMode(ams); - } - mAirplaneMode.mSettings = ams; - ProfileAirplaneModePreference amp = new ProfileAirplaneModePreference(getActivity()); - amp.setAirplaneModeItem(mAirplaneMode); - amp.setTitle(R.string.profile_airplanemode_title); - amp.setPersistent(false); - amp.setSummary(getActivity()); - amp.setOnPreferenceChangeListener(this); - mAirplaneMode.mCheckbox = amp; - systemPrefs.addPreference(amp); - - // Lockscreen mode preference - mScreenLockModePreference = new ListPreference(getActivity()); - mScreenLockModePreference.setTitle(R.string.profile_lockmode_title); - mScreenLockModePreference.setEntries(R.array.profile_lockmode_entries); - mScreenLockModePreference.setEntryValues(R.array.profile_lockmode_values); - mScreenLockModePreference.setPersistent(false); - mScreenLockModePreference.setSummary(getResources().getStringArray( - R.array.profile_lockmode_summaries)[mProfile.getScreenLockMode()]); - mScreenLockModePreference.setValue(String.valueOf(mProfile.getScreenLockMode())); - mScreenLockModePreference.setOnPreferenceChangeListener(this); - - // DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); - // if (dpm.requireSecureKeyguard()) { - // mScreenLockModePreference.setEnabled(false); - // mScreenLockModePreference.setSummary(R.string.unlock_set_unlock_disabled_summary); - // } - - systemPrefs.addPreference(mScreenLockModePreference); - } - - // Populate the audio streams list - final AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); - PreferenceGroup streamList = (PreferenceGroup) prefSet.findPreference("profile_volumeoverrides"); - if (streamList != null) { - streamList.removeAll(); - for (StreamItem stream : mStreams) { - StreamSettings settings = mProfile.getSettingsForStream(stream.mStreamId); - if (settings == null) { - settings = new StreamSettings(stream.mStreamId); - mProfile.setStreamSettings(settings); - } - stream.mSettings = settings; - StreamVolumePreference pref = new StreamVolumePreference(getActivity()); - pref.setKey("stream_" + stream.mStreamId); - pref.setTitle(stream.mLabel); - pref.setSummary(getString(R.string.volume_override_summary) + " " + settings.getValue() - + "/" + am.getStreamMaxVolume(stream.mStreamId)); - pref.setPersistent(false); - pref.setStreamItem(stream); - stream.mCheckbox = pref; - streamList.addPreference(pref); - } - } - - // Populate Connections list - PreferenceGroup connectionList = (PreferenceGroup) prefSet.findPreference("profile_connectionoverrides"); - if (connectionList != null) { - connectionList.removeAll(); - for (ConnectionItem connection : mConnections) { - String[] connectionstrings = getResources().getStringArray(connection.mChoices); - ConnectionSettings settings = mProfile.getSettingsForConnection(connection.mConnectionId); - if (settings == null) { - settings = new ConnectionSettings(connection.mConnectionId); - mProfile.setConnectionSettings(settings); - } - connection.mSettings = settings; - ProfileConnectionPreference pref = new ProfileConnectionPreference(getActivity()); - pref.setKey("connection_" + connection.mConnectionId); - pref.setTitle(connection.mLabel); - pref.setSummary(connectionstrings[settings.getValue()]); - pref.setPersistent(false); - pref.setConnectionItem(connection); - connection.mCheckbox = pref; - connectionList.addPreference(pref); - } - } - - // Populate Application groups - PreferenceGroup groupList = (PreferenceGroup) prefSet.findPreference("profile_appgroups"); - if (groupList != null) { - groupList.removeAll(); - for (ProfileGroup profileGroup : mProfile.getProfileGroups()) { - PreferenceScreen pref = new PreferenceScreen(getActivity(), null); - UUID uuid = profileGroup.getUuid(); - pref.setKey(uuid.toString()); - pref.setTitle(mProfileManager.getNotificationGroup(uuid).getName()); - //pref.setSummary(R.string.profile_summary); // summary is repetitive, consider removing - pref.setPersistent(false); - pref.setSelectable(true); - groupList.addPreference(pref); - } - } - } - - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if (preference instanceof StreamVolumePreference) { - for (StreamItem stream : mStreams) { - if (preference == stream.mCheckbox) { - stream.mSettings.setOverride((Boolean) newValue); - } - } - } else if (preference instanceof ProfileConnectionPreference) { - for (ConnectionItem connection : mConnections) { - if (preference == connection.mCheckbox) { - connection.mSettings.setOverride((Boolean) newValue); - } - } - } else if (preference == mRingMode.mCheckbox) { - mRingMode.mSettings.setOverride((Boolean) newValue); - } else if (preference == mAirplaneMode.mCheckbox) { - mAirplaneMode.mSettings.setOverride((Boolean) newValue); - } else if (preference == mNamePreference) { - String name = mNamePreference.getName().toString(); - if (!name.equals(mProfile.getName())) { - if (!mProfileManager.profileExists(name)) { - mProfile.setName(name); - } else { - mNamePreference.setName(mProfile.getName()); - Toast.makeText(getActivity(), R.string.duplicate_profile_name, Toast.LENGTH_LONG).show(); - } - } - } else if (preference == mScreenLockModePreference) { - mProfile.setScreenLockMode(Integer.valueOf((String) newValue)); - mScreenLockModePreference.setSummary(getResources().getStringArray( - R.array.profile_lockmode_summaries)[mProfile.getScreenLockMode()]); - } - return true; - } - - @Override - public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { - Log.d(TAG, "onPreferenceTreeClick(): entered" + preferenceScreen.getKey() + preference.getKey()); - if (preference instanceof PreferenceScreen) { - startProfileGroupActivity(preference.getKey(), preference.getTitle().toString()); - return true; - } - return super.onPreferenceTreeClick(preferenceScreen, preference); - } - - private void startProfileGroupActivity(String key, String title) { - Bundle args = new Bundle(); - args.putString("ProfileGroup", key.toString()); - args.putParcelable("Profile", mProfile); - - String header = mProfile.getName().toString() + ": " + title.toString(); - SettingsActivity pa = (SettingsActivity) getActivity(); - pa.startPreferencePanel(ProfileGroupConfig.class.getName(), args, - 0, header, this, PROFILE_GROUP_DETAILS); - } - - - private void deleteProfile() { - if (mProfile.getUuid().equals(mProfileManager.getActiveProfile().getUuid())) { - Toast toast = Toast.makeText(getActivity(), getString(R.string.profile_cannot_delete), - Toast.LENGTH_SHORT); - toast.show(); - } else { - AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); - alert.setTitle(R.string.profile_menu_delete); - alert.setIconAttribute(android.R.attr.alertDialogIcon); - alert.setMessage(R.string.profile_delete_confirm); - alert.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - doDelete(); - } - }); - alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - } - }); - alert.create().show(); - } - } - - private void doDelete() { - mProfileManager.removeProfile(mProfile); - mProfile = null; - finish(); - } - - static class StreamItem { - int mStreamId; - String mLabel; - StreamSettings mSettings; - StreamVolumePreference mCheckbox; - - public StreamItem(int streamId, String label) { - mStreamId = streamId; - mLabel = label; - } - } - - static class ConnectionItem { - int mConnectionId; - String mLabel; - ConnectionSettings mSettings; - ProfileConnectionPreference mCheckbox; - int mChoices; - - public ConnectionItem(int connectionId, String label) { - mConnectionId = connectionId; - mChoices = R.array.profile_connection_entries; - mLabel = label; - } - - public ConnectionItem(int connectionId, String label, int choices) { - mConnectionId = connectionId; - mLabel = label; - mChoices = choices; - } - } - - static class RingModeItem { - RingModeSettings mSettings; - ProfileRingModePreference mCheckbox; - - public RingModeItem() { - // nothing to do - } - } - - static class AirplaneModeItem { - AirplaneModeSettings mSettings; - ProfileAirplaneModePreference mCheckbox; - - public AirplaneModeItem() { - // nothing to do - } - } -} diff --git a/src/com/android/settings/profiles/ProfileConnectionPreference.java b/src/com/android/settings/profiles/ProfileConnectionPreference.java deleted file mode 100644 index a5fc986..0000000 --- a/src/com/android/settings/profiles/ProfileConnectionPreference.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * 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.profiles; - -import android.app.AlertDialog; -import android.app.Dialog; -import android.content.Context; -import android.content.DialogInterface; -import android.preference.Preference; -import android.util.AttributeSet; -import android.view.View; -import android.view.ViewGroup; -import android.widget.CheckBox; -import android.widget.CompoundButton; -import android.widget.LinearLayout; - -import com.android.settings.R; - -public class ProfileConnectionPreference extends Preference implements - CompoundButton.OnCheckedChangeListener, View.OnClickListener { - - private boolean mProtectFromCheckedChange = false; - - private CheckBox mCheckBox; - - final static String TAG = "ProfileConnectionPreference"; - - private ProfileConfig.ConnectionItem mConnectionItem; - - final static int defaultChoice = -1; - - private int currentChoice; - - /** - * @param context - * @param attrs - * @param defStyle - */ - public ProfileConnectionPreference(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - init(); - } - - /** - * @param context - * @param attrs - */ - public ProfileConnectionPreference(Context context, AttributeSet attrs) { - super(context, attrs); - init(); - } - - /** - * @param context - */ - public ProfileConnectionPreference(Context context) { - super(context); - init(); - } - - @Override - public View getView(View convertView, ViewGroup parent) { - View view = super.getView(convertView, parent); - - View widget = view.findViewById(R.id.profile_checkbox); - if ((widget != null) && widget instanceof CheckBox) { - mCheckBox = (CheckBox) widget; - mCheckBox.setOnCheckedChangeListener(this); - - mProtectFromCheckedChange = true; - mCheckBox.setChecked(isChecked()); - mProtectFromCheckedChange = false; - } - - View textLayout = view.findViewById(R.id.text_layout); - if ((textLayout != null) && textLayout instanceof LinearLayout) { - textLayout.setOnClickListener(this); - } - - return view; - } - - private void init() { - setLayoutResource(R.layout.preference_streamvolume); - } - - public boolean isChecked() { - return mConnectionItem != null && mConnectionItem.mSettings.isOverride(); - } - - public void setConnectionItem(ProfileConfig.ConnectionItem connectionItem) { - mConnectionItem = connectionItem; - - if (mCheckBox != null) { - mCheckBox.setChecked(mConnectionItem.mSettings.isOverride()); - } - } - - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (mProtectFromCheckedChange) { - return; - } - - mConnectionItem.mSettings.setOverride(isChecked); - - callChangeListener(isChecked); - } - - protected Dialog createConnectionDialog() { - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - final String[] ConnectionValues = getContext().getResources().getStringArray(R.array.profile_connection_values); - final String[] connectionNames = getContext().getResources().getStringArray(mConnectionItem.mChoices); - - currentChoice = mConnectionItem.mSettings.getValue(); - - builder.setTitle(mConnectionItem.mLabel); - builder.setSingleChoiceItems(mConnectionItem.mChoices, currentChoice, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int item) { - currentChoice = item; - } - }); - - builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int item) { - if (currentChoice != defaultChoice) { - int value = Integer.parseInt(ConnectionValues[currentChoice]); - mConnectionItem.mSettings.setValue(value); - setSummary(connectionNames[value]); - } - } - }); - - builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int item) { - } - }); - return builder.create(); - } - - public ProfileConfig.ConnectionItem getConnectionItem() { - return mConnectionItem; - } - - @Override - public void onClick(android.view.View v) { - if ((v != null) && (R.id.text_layout == v.getId())) { - createConnectionDialog().show(); - } - } -} diff --git a/src/com/android/settings/profiles/ProfileEnabler.java b/src/com/android/settings/profiles/ProfileEnabler.java index 0540dbe..487adc9 100644 --- a/src/com/android/settings/profiles/ProfileEnabler.java +++ b/src/com/android/settings/profiles/ProfileEnabler.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012 The CyanogenMod Project + * Copyright (C) 2010 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. @@ -17,51 +17,102 @@ package com.android.settings.profiles; import android.app.ProfileManager; +import android.content.BroadcastReceiver; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; +import android.database.ContentObserver; +import android.net.NetworkInfo; +import android.net.Uri; +import android.net.wifi.SupplicantState; +import android.net.wifi.WifiInfo; +import android.net.wifi.WifiManager; +import android.os.Handler; +import android.os.Message; import android.provider.Settings; -import android.widget.CompoundButton; import android.widget.Switch; +import android.widget.Toast; +import com.android.settings.R; +import com.android.settings.WirelessSettings; +import com.android.settings.search.Index; +import com.android.settings.widget.SwitchBar; +import com.android.settings.wifi.WifiSettings; + +import java.util.concurrent.atomic.AtomicBoolean; + +public class ProfileEnabler implements SwitchBar.OnSwitchChangeListener { + private Context mContext; + private SwitchBar mSwitchBar; + private SettingsObserver mSettingsObserver; + private boolean mListeningToOnSwitchChange = false; -public class ProfileEnabler implements CompoundButton.OnCheckedChangeListener { - private final Context mContext; - private Switch mSwitch; private boolean mStateMachineEvent; - public ProfileEnabler(Context context, Switch switch_) { + public ProfileEnabler(Context context, SwitchBar switchBar) { mContext = context; - mSwitch = switch_; + mSwitchBar = switchBar; + mSettingsObserver = new SettingsObserver(new Handler()); + setupSwitchBar(); } - public void resume() { - mSwitch.setOnCheckedChangeListener(this); + public void setupSwitchBar() { setSwitchState(); + if (!mListeningToOnSwitchChange) { + mSwitchBar.addOnSwitchChangeListener(this); + mListeningToOnSwitchChange = true; + } + mSwitchBar.show(); + } + + public void teardownSwitchBar() { + if (mListeningToOnSwitchChange) { + mSwitchBar.removeOnSwitchChangeListener(this); + mListeningToOnSwitchChange = false; + } + mSwitchBar.hide(); + } + + public void resume(Context context) { + mContext = context; + if (!mListeningToOnSwitchChange) { + mSwitchBar.addOnSwitchChangeListener(this); + mSettingsObserver.observe(); + + mListeningToOnSwitchChange = true; + } } public void pause() { - mSwitch.setOnCheckedChangeListener(null); + if (mListeningToOnSwitchChange) { + mSwitchBar.removeOnSwitchChangeListener(this); + mSettingsObserver.unobserve(); + + mListeningToOnSwitchChange = false; + } } - public void setSwitch(Switch switch_) { - if (mSwitch == switch_) return; - mSwitch.setOnCheckedChangeListener(null); - mSwitch = switch_; - mSwitch.setOnCheckedChangeListener(this); - setSwitchState(); + private void setSwitchBarChecked(boolean checked) { + mStateMachineEvent = true; + mSwitchBar.setChecked(checked); + mStateMachineEvent = false; } private void setSwitchState() { boolean enabled = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SYSTEM_PROFILES_ENABLED, 1) == 1; mStateMachineEvent = true; - mSwitch.setChecked(enabled); + setSwitchBarChecked(enabled); mStateMachineEvent = false; } - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + @Override + public void onSwitchChanged(Switch switchView, boolean isChecked) { + //Do nothing if called as a result of a state machine event if (mStateMachineEvent) { return; } + // Handle a switch change Settings.System.putInt(mContext.getContentResolver(), Settings.System.SYSTEM_PROFILES_ENABLED, isChecked ? 1 : 0); @@ -72,10 +123,40 @@ public class ProfileEnabler implements CompoundButton.OnCheckedChangeListener { intent.putExtra( ProfileManager.EXTRA_PROFILES_STATE, isChecked ? - ProfileManager.PROFILES_STATE_ENABLED : - ProfileManager.PROFILES_STATE_DISABLED); + ProfileManager.PROFILES_STATE_ENABLED : + ProfileManager.PROFILES_STATE_DISABLED); mContext.sendBroadcast(intent); - } + class SettingsObserver extends ContentObserver { + SettingsObserver(Handler handler) { + super(handler); + } + + void observe() { + ContentResolver resolver = mContext.getContentResolver(); + resolver.registerContentObserver(Settings.System.getUriFor( + Settings.System.SYSTEM_PROFILES_ENABLED), false, this); + update(); + } + + void unobserve() { + ContentResolver resolver = mContext.getContentResolver(); + resolver.unregisterContentObserver(this); + } + + @Override + public void onChange(boolean selfChange) { + update(); + } + + @Override + public void onChange(boolean selfChange, Uri uri) { + update(); + } + + public void update() { + setSwitchState(); + } + } } diff --git a/src/com/android/settings/profiles/ProfileGroupConfig.java b/src/com/android/settings/profiles/ProfileGroupConfig.java deleted file mode 100644 index f1d4e7b..0000000 --- a/src/com/android/settings/profiles/ProfileGroupConfig.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * 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.profiles; - -import java.util.UUID; - -import android.app.Profile; -import android.app.ProfileGroup; -import android.app.ProfileGroup.Mode; -import android.app.ProfileManager; -import android.net.Uri; -import android.os.Bundle; -import android.preference.ListPreference; -import android.preference.Preference; -import android.preference.Preference.OnPreferenceChangeListener; - -import com.android.settings.R; -import com.android.settings.SettingsPreferenceFragment; - -public class ProfileGroupConfig extends SettingsPreferenceFragment implements - OnPreferenceChangeListener { - - public static final String PROFILE_SERVICE = "profile"; - - private static final CharSequence KEY_SOUNDMODE = "sound_mode"; - - private static final CharSequence KEY_VIBRATEMODE = "vibrate_mode"; - - private static final CharSequence KEY_LIGHTSMODE = "lights_mode"; - - private static final CharSequence KEY_RINGERMODE = "ringer_mode"; - - private static final CharSequence KEY_SOUNDTONE = "soundtone"; - - private static final CharSequence KEY_RINGTONE = "ringtone"; - - Profile mProfile; - - ProfileGroup mProfileGroup; - - private ListPreference mSoundMode; - - private ListPreference mRingerMode; - - private ListPreference mVibrateMode; - - private ListPreference mLightsMode; - - private ProfileRingtonePreference mRingTone; - - private ProfileRingtonePreference mSoundTone; - - private ProfileManager mProfileManager; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - addPreferencesFromResource(R.xml.profile_settings); - - final Bundle args = getArguments(); - if (args != null) { - mProfile = (Profile) args.getParcelable("Profile"); - UUID uuid = UUID.fromString(args.getString("ProfileGroup")); - - mProfileManager = (ProfileManager) getSystemService(PROFILE_SERVICE); - mProfileGroup = mProfile.getProfileGroup(uuid); - - mRingerMode = (ListPreference) findPreference(KEY_RINGERMODE); - mSoundMode = (ListPreference) findPreference(KEY_SOUNDMODE); - mVibrateMode = (ListPreference) findPreference(KEY_VIBRATEMODE); - mLightsMode = (ListPreference) findPreference(KEY_LIGHTSMODE); - mRingTone = (ProfileRingtonePreference) findPreference(KEY_RINGTONE); - mSoundTone = (ProfileRingtonePreference) findPreference(KEY_SOUNDTONE); - - mRingTone.setShowSilent(false); - mSoundTone.setShowSilent(false); - - mSoundMode.setOnPreferenceChangeListener(this); - mRingerMode.setOnPreferenceChangeListener(this); - mVibrateMode.setOnPreferenceChangeListener(this); - mLightsMode.setOnPreferenceChangeListener(this); - mSoundTone.setOnPreferenceChangeListener(this); - mRingTone.setOnPreferenceChangeListener(this); - - updateState(); - } - } - - private void updateState() { - - mVibrateMode.setValue(mProfileGroup.getVibrateMode().name()); - mSoundMode.setValue(mProfileGroup.getSoundMode().name()); - mRingerMode.setValue(mProfileGroup.getRingerMode().name()); - mLightsMode.setValue(mProfileGroup.getLightsMode().name()); - - mVibrateMode.setSummary(mVibrateMode.getEntry()); - mSoundMode.setSummary(mSoundMode.getEntry()); - mRingerMode.setSummary(mRingerMode.getEntry()); - mLightsMode.setSummary(mLightsMode.getEntry()); - - if (mProfileGroup.getSoundOverride() != null) { - mSoundTone.setRingtone(mProfileGroup.getSoundOverride()); - } - - if (mProfileGroup.getRingerOverride() != null) { - mRingTone.setRingtone(mProfileGroup.getRingerOverride()); - } - - } - - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if (preference == mVibrateMode) { - mProfileGroup.setVibrateMode(Mode.valueOf((String) newValue)); - } else if (preference == mSoundMode) { - mProfileGroup.setSoundMode(Mode.valueOf((String) newValue)); - } else if (preference == mRingerMode) { - mProfileGroup.setRingerMode(Mode.valueOf((String) newValue)); - } else if (preference == mLightsMode) { - mProfileGroup.setLightsMode(Mode.valueOf((String) newValue)); - } else if (preference == mRingTone) { - Uri uri = Uri.parse((String) newValue); - mProfileGroup.setRingerOverride(uri); - } else if (preference == mSoundTone) { - Uri uri = Uri.parse((String) newValue); - mProfileGroup.setSoundOverride(uri); - } - - mProfileManager.updateProfile(mProfile); - - updateState(); - return true; - } -} diff --git a/src/com/android/settings/profiles/ProfileRingModePreference.java b/src/com/android/settings/profiles/ProfileRingModePreference.java deleted file mode 100644 index d471998..0000000 --- a/src/com/android/settings/profiles/ProfileRingModePreference.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * 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.profiles; - -import android.app.AlertDialog; -import android.app.Dialog; -import android.content.Context; -import android.content.DialogInterface; -import android.preference.Preference; -import android.util.AttributeSet; -import android.view.View; -import android.view.ViewGroup; -import android.widget.CheckBox; -import android.widget.CompoundButton; -import android.widget.LinearLayout; - -import com.android.settings.R; - -public class ProfileRingModePreference extends Preference implements - CompoundButton.OnCheckedChangeListener, View.OnClickListener { - - private boolean mProtectFromCheckedChange = false; - - private CheckBox mCheckBox; - - final static String TAG = "ProfileRingModePreference"; - - private ProfileConfig.RingModeItem mRingModeItem; - - final static int defaultChoice = -1; - - private int currentChoice; - - /** - * @param context - * @param attrs - * @param defStyle - */ - public ProfileRingModePreference(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - init(); - } - - /** - * @param context - * @param attrs - */ - public ProfileRingModePreference(Context context, AttributeSet attrs) { - super(context, attrs); - init(); - } - - /** - * @param context - */ - public ProfileRingModePreference(Context context) { - super(context); - init(); - } - - @Override - public View getView(View convertView, ViewGroup parent) { - View view = super.getView(convertView, parent); - - View widget = view.findViewById(R.id.profile_checkbox); - if ((widget != null) && widget instanceof CheckBox) { - mCheckBox = (CheckBox) widget; - mCheckBox.setOnCheckedChangeListener(this); - - mProtectFromCheckedChange = true; - mCheckBox.setChecked(isChecked()); - mProtectFromCheckedChange = false; - } - - View textLayout = view.findViewById(R.id.text_layout); - if ((textLayout != null) && textLayout instanceof LinearLayout) { - textLayout.setOnClickListener(this); - } - - return view; - } - - private void init() { - setLayoutResource(R.layout.preference_streamvolume); - } - - public boolean isChecked() { - return mRingModeItem != null && mRingModeItem.mSettings.isOverride(); - } - - public void setRingModeItem(ProfileConfig.RingModeItem ringModeItem) { - mRingModeItem = ringModeItem; - - if (mCheckBox != null) { - mCheckBox.setChecked(mRingModeItem.mSettings.isOverride()); - } - } - - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (mProtectFromCheckedChange) { - return; - } - - mRingModeItem.mSettings.setOverride(isChecked); - - callChangeListener(isChecked); - } - - protected Dialog createRingModeDialog() { - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - final String[] ringModeValues = getContext().getResources().getStringArray(R.array.ring_mode_values); - String currentValue = mRingModeItem.mSettings.getValue(); - if (currentValue != null) { - for (int i = 0; i < ringModeValues.length; i++) { - if (currentValue.equals(ringModeValues[i])) { - currentChoice = i; - break; - } - } - } - - builder.setTitle(R.string.ring_mode_title); - builder.setSingleChoiceItems(R.array.ring_mode_entries, currentChoice, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int item) { - currentChoice = item; - } - }); - - builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int item) { - if (currentChoice != defaultChoice) { - String value = ringModeValues[currentChoice]; - mRingModeItem.mSettings.setValue(value); - setSummary(getContext().getResources().getStringArray(R.array.ring_mode_entries)[currentChoice]); - } - } - }); - - builder.setNegativeButton(android.R.string.cancel, null); - return builder.create(); - } - - public ProfileConfig.RingModeItem getRingModeItem() { - return mRingModeItem; - } - - @Override - public void onClick(android.view.View v) { - if ((v != null) && (R.id.text_layout == v.getId())) { - createRingModeDialog().show(); - } - } - - public void setSummary(Context context) { - String[] entries = context.getResources().getStringArray(R.array.ring_mode_entries); - String[] values = context.getResources().getStringArray(R.array.ring_mode_values); - int l = entries.length; - String value = mRingModeItem.mSettings.getValue(); - for (int i = 0; i < l; i++) { - if (value.equals(values[i])) { - setSummary(entries[i]); - break; - } - } - } -} diff --git a/src/com/android/settings/profiles/ProfileRingtonePreference.java b/src/com/android/settings/profiles/ProfileRingtonePreference.java deleted file mode 100644 index 91ccbe6..0000000 --- a/src/com/android/settings/profiles/ProfileRingtonePreference.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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.profiles; - -import android.content.Context; -import android.content.Intent; -import android.media.RingtoneManager; -import android.net.Uri; -import android.preference.RingtonePreference; -import android.util.AttributeSet; - -public class ProfileRingtonePreference extends RingtonePreference { - private static final String TAG = "ProfileRingtonePreference"; - - public ProfileRingtonePreference(Context context, AttributeSet attrs) { - super(context, attrs); - } - - @Override - protected void onPrepareRingtonePickerIntent(Intent ringtonePickerIntent) { - super.onPrepareRingtonePickerIntent(ringtonePickerIntent); - - /* - * Since this preference is for choosing the default ringtone, it - * doesn't make sense to show a 'Default' item. - */ - ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false); - } - - private Uri mRingtone; - - void setRingtone(Uri uri) { - mRingtone = uri; - } - - @Override - protected Uri onRestoreRingtone() { - if (mRingtone == null) { - return super.onRestoreRingtone(); - } else { - return mRingtone; - } - } -} diff --git a/src/com/android/settings/profiles/ProfilesList.java b/src/com/android/settings/profiles/ProfilesList.java index 13fa16b..29c7500 100644 --- a/src/com/android/settings/profiles/ProfilesList.java +++ b/src/com/android/settings/profiles/ProfilesList.java @@ -20,10 +20,10 @@ import java.util.UUID; import android.app.Profile; import android.app.ProfileManager; +import android.content.Context; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceScreen; -import android.provider.Settings; import android.text.TextUtils; import com.android.settings.R; @@ -33,7 +33,6 @@ import com.android.settings.Utils; public class ProfilesList extends SettingsPreferenceFragment implements Preference.OnPreferenceChangeListener { static final String TAG = "ProfilesSettings"; - public static final String PROFILE_SERVICE = "profile"; private ProfileManager mProfileManager; @@ -42,7 +41,7 @@ public class ProfilesList extends SettingsPreferenceFragment implements super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.profiles_settings); - mProfileManager = (ProfileManager) getActivity().getSystemService(PROFILE_SERVICE); + mProfileManager = (ProfileManager) getActivity().getSystemService(Context.PROFILE_SERVICE); } @Override @@ -66,7 +65,8 @@ public class ProfilesList extends SettingsPreferenceFragment implements for (Profile profile : mProfileManager.getProfiles()) { Bundle args = new Bundle(); - args.putParcelable("Profile", profile); + args.putParcelable(ProfilesSettings.EXTRA_PROFILE, profile); + args.putBoolean(ProfilesSettings.EXTRA_NEW_PROFILE, false); ProfilesPreference ppref = new ProfilesPreference(this, args); ppref.setKey(profile.getUuid().toString()); diff --git a/src/com/android/settings/profiles/ProfilesPreference.java b/src/com/android/settings/profiles/ProfilesPreference.java index 91f57ad..2695144 100644 --- a/src/com/android/settings/profiles/ProfilesPreference.java +++ b/src/com/android/settings/profiles/ProfilesPreference.java @@ -19,17 +19,16 @@ package com.android.settings.profiles; import android.content.ActivityNotFoundException; import android.os.Bundle; import android.preference.CheckBoxPreference; -import android.preference.PreferenceActivity; import android.view.View; -import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.TextView; import com.android.settings.R; import com.android.settings.SettingsActivity; import com.android.settings.SettingsPreferenceFragment; +import com.android.settings.SubSettings; -public class ProfilesPreference extends CheckBoxPreference { +public class ProfilesPreference extends CheckBoxPreference implements View.OnClickListener { private static final String TAG = ProfilesPreference.class.getSimpleName(); private static final float DISABLED_ALPHA = 0.4f; private final SettingsPreferenceFragment mFragment; @@ -43,17 +42,6 @@ public class ProfilesPreference extends CheckBoxPreference { private TextView mSummaryText; private View mProfilesPref; - private final OnClickListener mPrefOnclickListener = new OnClickListener() { - @Override - public void onClick(View arg0) { - if (!isEnabled() || isChecked()) { - return; - } - setChecked(true); - callChangeListener(getKey()); - } - }; - public ProfilesPreference(SettingsPreferenceFragment fragment, Bundle settingsBundle) { super(fragment.getActivity(), null, R.style.ProfilesPreferenceStyle); setLayoutResource(R.layout.preference_profiles); @@ -65,30 +53,34 @@ public class ProfilesPreference extends CheckBoxPreference { @Override protected void onBindView(View view) { super.onBindView(view); + mProfilesPref = view.findViewById(R.id.profiles_pref); - mProfilesPref.setOnClickListener(mPrefOnclickListener); + mProfilesPref.setOnClickListener(this); mProfilesSettingsButton = (ImageView)view.findViewById(R.id.profiles_settings); mTitleText = (TextView)view.findViewById(android.R.id.title); mSummaryText = (TextView)view.findViewById(android.R.id.summary); if (mSettingsBundle != null) { - mProfilesSettingsButton.setOnClickListener( - new OnClickListener() { - @Override - public void onClick(View arg0) { - try { - startProfileConfigActivity(); - } catch (ActivityNotFoundException e) { - // If the settings activity does not exist, we can just - // do nothing... - } - } - }); - } - if (mSettingsBundle == null) { - mProfilesSettingsButton.setVisibility(View.GONE); - } else { + mProfilesSettingsButton.setOnClickListener(this); updatePreferenceViews(); + } else { + mProfilesSettingsButton.setVisibility(View.GONE); + } + } + + @Override + public void onClick(View view) { + if (view == mProfilesSettingsButton) { + try { + startProfileConfigActivity(); + } catch (ActivityNotFoundException e) { + // If the settings activity does not exist, we can just do nothing... + } + } else if (view == mProfilesPref) { + if (isEnabled() && !isChecked()) { + setChecked(true); + callChangeListener(getKey()); + } } } @@ -130,7 +122,7 @@ public class ProfilesPreference extends CheckBoxPreference { mProfilesPref.setEnabled(true); mProfilesPref.setLongClickable(checked); final boolean enabled = isEnabled(); - mProfilesPref.setOnClickListener(enabled ? mPrefOnclickListener : null); + mProfilesPref.setOnClickListener(enabled ? this : null); if (!enabled) { mProfilesPref.setBackgroundColor(0); } @@ -139,13 +131,8 @@ public class ProfilesPreference extends CheckBoxPreference { // utility method used to start sub activity private void startProfileConfigActivity() { - SettingsActivity pa = (SettingsActivity) mFragment.getActivity(); - pa.startPreferencePanel(ProfileConfig.class.getName(), mSettingsBundle, - R.string.profile_profile_manage, null, mFragment, PROFILE_DETAILS); - } - - @Override - public void setChecked(boolean checked) { - super.setChecked(checked); + SubSettings pa = (SubSettings) mFragment.getActivity(); + pa.startPreferencePanel(SetupActionsFragment.class.getCanonicalName(), mSettingsBundle, + R.string.profile_profile_manage, null, null, PROFILE_DETAILS); } } diff --git a/src/com/android/settings/profiles/ProfilesSettings.java b/src/com/android/settings/profiles/ProfilesSettings.java index 2576858..7f259b2 100644 --- a/src/com/android/settings/profiles/ProfilesSettings.java +++ b/src/com/android/settings/profiles/ProfilesSettings.java @@ -21,7 +21,6 @@ import android.app.Activity; import android.app.AlertDialog; import android.app.Fragment; import android.app.FragmentManager; -import android.app.NotificationGroup; import android.app.Profile; import android.app.ProfileManager; import android.content.BroadcastReceiver; @@ -30,11 +29,10 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; -import android.preference.PreferenceActivity; import android.provider.Settings; -import android.support.v4.view.PagerTabStrip; import android.support.v4.view.ViewPager; import android.support.v13.app.FragmentStatePagerAdapter; +import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; @@ -42,25 +40,23 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.CompoundButton; -import android.widget.EditText; +import android.widget.ImageView; import android.widget.Switch; import android.widget.TextView; -import android.widget.Toast; import com.android.settings.R; +import com.android.settings.SettingsActivity; import com.android.settings.SettingsPreferenceFragment; +import com.android.settings.SubSettings; import com.android.settings.Utils; -import java.util.HashMap; - public class ProfilesSettings extends SettingsPreferenceFragment { - private static final String TAG = "ProfilesSettings"; - private static final String PROFILE_SERVICE = "profile"; + + public static final String EXTRA_PROFILE = "Profile"; + public static final String EXTRA_NEW_PROFILE = "new_profile_mode"; private static final int MENU_RESET = Menu.FIRST; - private static final int MENU_ADD = Menu.FIRST + 1; private final IntentFilter mFilter; private final BroadcastReceiver mReceiver; @@ -68,11 +64,10 @@ public class ProfilesSettings extends SettingsPreferenceFragment { private ProfileManager mProfileManager; private ProfileEnabler mProfileEnabler; - private Switch mActionBarSwitch; - private ViewPager mViewPager; private TextView mEmptyText; private ProfilesPagerAdapter mAdapter; + private ImageView mAddProfileFab; private boolean mEnabled; ViewGroup mContainer; @@ -92,6 +87,8 @@ public class ProfilesSettings extends SettingsPreferenceFragment { } } }; + + setHasOptionsMenu(true); } @Override @@ -102,47 +99,24 @@ public class ProfilesSettings extends SettingsPreferenceFragment { View view = inflater.inflate(R.layout.profile_tabs, container, false); mViewPager = (ViewPager) view.findViewById(R.id.pager); mEmptyText = (TextView) view.findViewById(R.id.empty); - - mAdapter = new ProfilesPagerAdapter(getFragmentManager()); + mAddProfileFab = (ImageView) view.findViewById(R.id.floating_action_button); + mAddProfileFab.setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + addProfile(); + } + }); + + mAdapter = new ProfilesPagerAdapter(getChildFragmentManager()); mViewPager.setAdapter(mAdapter); - PagerTabStrip tabs = (PagerTabStrip) view.findViewById(R.id.tabs); - tabs.setTabIndicatorColorResource(android.R.color.holo_blue_light); - - mProfileManager = (ProfileManager) getActivity().getSystemService(PROFILE_SERVICE); - - setHasOptionsMenu(true); - return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { - // We don't call super.onActivityCreated() here, since it assumes we already set up - // Preference (probably in onCreate()), while ProfilesSettings exceptionally set it up in - // this method. - // On/off switch - Activity activity = getActivity(); - //Switch - mActionBarSwitch = new Switch(activity); - - if (activity instanceof PreferenceActivity) { - PreferenceActivity preferenceActivity = (PreferenceActivity) activity; - if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) { - final int padding = activity.getResources().getDimensionPixelSize( - R.dimen.action_bar_switch_padding); - mActionBarSwitch.setPaddingRelative(0, 0, padding, 0); - activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, - ActionBar.DISPLAY_SHOW_CUSTOM); - activity.getActionBar().setCustomView(mActionBarSwitch, new ActionBar.LayoutParams( - ActionBar.LayoutParams.WRAP_CONTENT, - ActionBar.LayoutParams.WRAP_CONTENT, - Gravity.CENTER_VERTICAL | Gravity.END)); - } - } - - mProfileEnabler = new ProfileEnabler(activity, mActionBarSwitch); - + mProfileManager = (ProfileManager) getActivity().getSystemService(Context.PROFILE_SERVICE); // After confirming PreferenceScreen is available, we call super. super.onActivityCreated(savedInstanceState); } @@ -151,7 +125,7 @@ public class ProfilesSettings extends SettingsPreferenceFragment { public void onResume() { super.onResume(); if (mProfileEnabler != null) { - mProfileEnabler.resume(); + mProfileEnabler.resume(getActivity()); } getActivity().registerReceiver(mReceiver, mFilter); @@ -174,20 +148,27 @@ public class ProfilesSettings extends SettingsPreferenceFragment { } @Override + public void onStart() { + super.onStart(); + final SettingsActivity activity = (SettingsActivity) getActivity(); + mProfileEnabler = new ProfileEnabler(activity, activity.getSwitchBar()); + } + + @Override + public void onDestroyView() { + if (mProfileEnabler != null) { + mProfileEnabler.teardownSwitchBar(); + } + super.onDestroyView(); + } + + @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + super.onCreateOptionsMenu(menu, inflater); menu.add(0, MENU_RESET, 0, R.string.profile_reset_title) - .setIcon(R.drawable.ic_settings_backup) // use the backup icon .setAlphabeticShortcut('r') .setEnabled(mEnabled) - .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | - MenuItem.SHOW_AS_ACTION_WITH_TEXT); - - menu.add(0, MENU_ADD, 0, R.string.profiles_add) - .setIcon(R.drawable.ic_menu_add) - .setAlphabeticShortcut('a') - .setEnabled(mEnabled) - .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | - MenuItem.SHOW_AS_ACTION_WITH_TEXT); + .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); } @Override @@ -196,99 +177,34 @@ public class ProfilesSettings extends SettingsPreferenceFragment { case MENU_RESET: resetAll(); return true; - - case MENU_ADD: - // determine dialog to launch - if (mViewPager.getCurrentItem() == 0) { - addProfile(); - } else { - addAppGroup(); - } - return true; - - default: - return false; } + return super.onOptionsItemSelected(item); } private void addProfile() { - LayoutInflater inflater = getActivity().getLayoutInflater(); - View content = inflater.inflate(R.layout.profile_name_dialog, null); - final TextView prompt = (TextView) content.findViewById(R.id.prompt); - final EditText entry = (EditText) content.findViewById(R.id.name); - - prompt.setText(R.string.profile_profile_name_prompt); - - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - builder.setTitle(R.string.menu_new_profile); - builder.setView(content); - - builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - String name = entry.getText().toString(); - if (!mProfileManager.profileExists(name)) { - Profile profile = new Profile(name); - mProfileManager.addProfile(profile); - mAdapter.refreshProfiles(); - } else { - Toast.makeText(getActivity(), - R.string.duplicate_profile_name, Toast.LENGTH_LONG).show(); - } - } - }); - builder.setNegativeButton(android.R.string.cancel, null); + Bundle args = new Bundle(); + args.putBoolean(EXTRA_NEW_PROFILE, true); + args.putParcelable(EXTRA_PROFILE, new Profile(getString(R.string.new_profile_name))); - AlertDialog dialog = builder.create(); - dialog.show(); + SubSettings pa = (SubSettings) getActivity(); + pa.startPreferencePanel(SetupTriggersFragment.class.getCanonicalName(), args, + 0, null, this, 0); } private void resetAll() { - AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); - alert.setTitle(R.string.profile_reset_title); - alert.setIconAttribute(android.R.attr.alertDialogIcon); - alert.setMessage(R.string.profile_reset_message); - alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - mProfileManager.resetAll(); - mAdapter.refreshProfiles(); - mAdapter.refreshAppGroups(); - } - }); - alert.setNegativeButton(R.string.cancel, null); - alert.show(); - } - - private void addAppGroup() { - LayoutInflater inflater = getActivity().getLayoutInflater(); - View content = inflater.inflate(R.layout.profile_name_dialog, null); - final TextView prompt = (TextView) content.findViewById(R.id.prompt); - final EditText entry = (EditText) content.findViewById(R.id.name); - - prompt.setText(R.string.profile_appgroup_name_prompt); - - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - builder.setTitle(R.string.profile_new_appgroup); - builder.setView(content); - - builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - String name = entry.getText().toString(); - if (!mProfileManager.notificationGroupExists(name)) { - NotificationGroup newGroup = new NotificationGroup(name); - mProfileManager.addNotificationGroup(newGroup); - mAdapter.refreshAppGroups(); - } else { - Toast.makeText(getActivity(), - R.string.duplicate_appgroup_name, Toast.LENGTH_LONG).show(); - } - } - }); - builder.setNegativeButton(android.R.string.cancel, null); - - AlertDialog dialog = builder.create(); - dialog.show(); + new AlertDialog.Builder(getActivity()) + .setTitle(R.string.profile_reset_title) + .setIconAttribute(android.R.attr.alertDialogIcon) + .setMessage(R.string.profile_reset_message) + .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + mProfileManager.resetAll(); + mAdapter.refreshProfiles(); + } + }) + .setNegativeButton(R.string.cancel, null) + .show(); } private void updateProfilesEnabledState() { @@ -298,14 +214,14 @@ public class ProfilesSettings extends SettingsPreferenceFragment { Settings.System.SYSTEM_PROFILES_ENABLED, 1) == 1; activity.invalidateOptionsMenu(); + mAddProfileFab.setVisibility(mEnabled ? View.VISIBLE : View.GONE); mViewPager.setVisibility(mEnabled ? View.VISIBLE : View.GONE); mEmptyText.setVisibility(mEnabled ? View.GONE : View.VISIBLE); } class ProfilesPagerAdapter extends FragmentStatePagerAdapter { - Fragment[] frags = { new ProfilesList(), new AppGroupList() }; - String[] titles = { getString(R.string.profile_profiles_manage), - getString(R.string.profile_appgroups_manage) }; + Fragment[] frags = { new ProfilesList() }; + String[] titles = { getString(R.string.profile_profiles_manage) }; ProfilesPagerAdapter(FragmentManager fm) { super(fm); @@ -330,8 +246,5 @@ public class ProfilesSettings extends SettingsPreferenceFragment { ((ProfilesList) frags[0]).refreshList(); } - public void refreshAppGroups() { - ((AppGroupList) frags[1]).refreshList(); - } } } diff --git a/src/com/android/settings/profiles/SetupActionsFragment.java b/src/com/android/settings/profiles/SetupActionsFragment.java new file mode 100644 index 0000000..230d84a --- /dev/null +++ b/src/com/android/settings/profiles/SetupActionsFragment.java @@ -0,0 +1,565 @@ +/* + * Copyright (C) 2014 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.Activity; +import android.app.AirplaneModeSettings; +import android.app.AlertDialog; +import android.app.ConnectionSettings; +import android.app.Dialog; +import android.app.Fragment; +import android.app.Profile; +import android.app.ProfileManager; +import android.app.RingModeSettings; +import android.app.StreamSettings; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.media.AudioManager; +import android.media.RingtoneManager; +import android.net.wimax.WimaxHelper; +import android.os.Bundle; +import android.telephony.TelephonyManager; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.EditText; +import android.widget.ListView; +import android.widget.SeekBar; +import android.widget.TextView; +import com.android.settings.R; +import com.android.settings.SubSettings; +import com.android.settings.cyanogenmod.DeviceUtils; +import com.android.settings.SettingsPreferenceFragment; +import com.android.settings.profiles.actions.ItemListAdapter; +import com.android.settings.profiles.actions.item.AirplaneModeItem; +import com.android.settings.profiles.actions.item.ConnectionOverrideItem; +import com.android.settings.profiles.actions.item.Header; +import com.android.settings.profiles.actions.item.Item; +import com.android.settings.profiles.actions.item.LockModeItem; +import com.android.settings.profiles.actions.item.ProfileNameItem; +import com.android.settings.profiles.actions.item.RingModeItem; +import com.android.settings.profiles.actions.item.VolumeStreamItem; + +import java.util.ArrayList; +import java.util.List; + +import static android.app.ConnectionSettings.PROFILE_CONNECTION_2G3G; +import static android.app.ConnectionSettings.PROFILE_CONNECTION_BLUETOOTH; +import static android.app.ConnectionSettings.PROFILE_CONNECTION_GPS; +import static android.app.ConnectionSettings.PROFILE_CONNECTION_MOBILEDATA; +import static android.app.ConnectionSettings.PROFILE_CONNECTION_NFC; +import static android.app.ConnectionSettings.PROFILE_CONNECTION_SYNC; +import static android.app.ConnectionSettings.PROFILE_CONNECTION_WIFI; +import static android.app.ConnectionSettings.PROFILE_CONNECTION_WIFIAP; +import static android.app.ConnectionSettings.PROFILE_CONNECTION_WIMAX; + +public class SetupActionsFragment extends SettingsPreferenceFragment + implements AdapterView.OnItemClickListener { + + private static final int RINGTONE_REQUEST_CODE = 1000; + + private static final int MENU_REMOVE = Menu.FIRST; + private static final int MENU_TRIGGERS = Menu.FIRST + 1; + + Profile mProfile; + ItemListAdapter mAdapter; + ProfileManager mProfileManager; + ListView mListView; + + boolean mNewProfileMode; + + private static final int[] LOCKMODE_MAPPING = new int[] { + Profile.LockMode.DEFAULT, Profile.LockMode.INSECURE, Profile.LockMode.DISABLE + }; + private static final int[] EXPANDED_DESKTOP_MAPPING = new int[] { + Profile.ExpandedDesktopMode.DEFAULT, + Profile.ExpandedDesktopMode.ENABLE, + Profile.ExpandedDesktopMode.DISABLE + }; + + public static SetupActionsFragment newInstance(Profile profile, boolean newProfile) { + SetupActionsFragment fragment = new SetupActionsFragment(); + Bundle args = new Bundle(); + args.putParcelable(ProfilesSettings.EXTRA_PROFILE, profile); + args.putBoolean(ProfilesSettings.EXTRA_NEW_PROFILE, newProfile); + + fragment.setArguments(args); + return fragment; + } + + public SetupActionsFragment() { + // Required empty public constructor + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mProfile = getArguments().getParcelable(ProfilesSettings.EXTRA_PROFILE); + mNewProfileMode = getArguments().getBoolean(ProfilesSettings.EXTRA_NEW_PROFILE, false); + } + + mProfileManager = (ProfileManager) getActivity().getSystemService(Context.PROFILE_SERVICE); + List<Item> items = new ArrayList<Item>(); + // general prefs + items.add(new Header(getString(R.string.profile_name_title))); + items.add(new ProfileNameItem(mProfile)); + + // connection overrides + items.add(new Header(getString(R.string.profile_connectionoverrides_title))); + if (DeviceUtils.deviceSupportsBluetooth()) { + items.add(new ConnectionOverrideItem(PROFILE_CONNECTION_BLUETOOTH, + mProfile.getSettingsForConnection(PROFILE_CONNECTION_BLUETOOTH))); + } + items.add(generateConnectionOverrideItem(PROFILE_CONNECTION_GPS)); + items.add(generateConnectionOverrideItem(PROFILE_CONNECTION_WIFI)); + items.add(generateConnectionOverrideItem(PROFILE_CONNECTION_SYNC)); + if (DeviceUtils.deviceSupportsMobileData(getActivity())) { + items.add(generateConnectionOverrideItem(PROFILE_CONNECTION_MOBILEDATA)); + items.add(generateConnectionOverrideItem(PROFILE_CONNECTION_WIFIAP)); + + final TelephonyManager tm = + (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE); + if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) { + items.add(generateConnectionOverrideItem(PROFILE_CONNECTION_2G3G)); + } + } + if (WimaxHelper.isWimaxSupported(getActivity())) { + items.add(generateConnectionOverrideItem(PROFILE_CONNECTION_WIMAX)); + } + if (DeviceUtils.deviceSupportsNfc(getActivity())) { + items.add(generateConnectionOverrideItem(PROFILE_CONNECTION_NFC)); + } + + // add volume streams + items.add(new Header(getString(R.string.profile_volumeoverrides_title))); + items.add(generateVolumeStreamItem(AudioManager.STREAM_ALARM)); + items.add(generateVolumeStreamItem(AudioManager.STREAM_MUSIC)); + items.add(generateVolumeStreamItem(AudioManager.STREAM_RING)); + items.add(generateVolumeStreamItem(AudioManager.STREAM_NOTIFICATION)); + + // system settings + items.add(new Header(getString(R.string.profile_system_settings_title))); + items.add(new RingModeItem(mProfile.getRingMode())); + items.add(new AirplaneModeItem(mProfile.getAirplaneMode())); + items.add(new LockModeItem(mProfile)); + + mAdapter = new ItemListAdapter(getActivity(), items); + + setHasOptionsMenu(true); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + super.onCreateOptionsMenu(menu, inflater); + if (!mNewProfileMode) { + menu.add(0, MENU_REMOVE, 0, R.string.profile_menu_delete_title) + .setIcon(R.drawable.ic_menu_trash_holo_dark) + .setAlphabeticShortcut('d') + .setEnabled(true) + .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | + MenuItem.SHOW_AS_ACTION_WITH_TEXT); + + menu.add(0, MENU_TRIGGERS, 0, R.string.profile_menu_triggers_title) + .setIcon(R.drawable.ic_location) + .setAlphabeticShortcut('t') + .setEnabled(true) + .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | + MenuItem.SHOW_AS_ACTION_WITH_TEXT); + } + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case MENU_REMOVE: + mProfileManager.removeProfile(mProfile); + finishFragment(); + return true; + + case MENU_TRIGGERS: + Bundle args = new Bundle(); + args.putParcelable(ProfilesSettings.EXTRA_PROFILE, mProfile); + args.putBoolean(ProfilesSettings.EXTRA_NEW_PROFILE, false); + + SubSettings pa = (SubSettings) getActivity(); + pa.startPreferencePanel(SetupTriggersFragment.class.getCanonicalName(), args, + R.string.profile_profile_manage, null, null, 0); + + return true; + } + return super.onOptionsItemSelected(item); + } + + private ConnectionOverrideItem generateConnectionOverrideItem(int connectionId) { + ConnectionSettings settings = mProfile.getSettingsForConnection(connectionId); + if (settings == null) { + settings = new ConnectionSettings(connectionId); + mProfile.setConnectionSettings(settings); + } + return new ConnectionOverrideItem(connectionId, settings); + } + + private VolumeStreamItem generateVolumeStreamItem(int stream) { + StreamSettings settings = mProfile.getSettingsForStream(stream); + if (settings == null) { + settings = new StreamSettings(stream); + mProfile.setStreamSettings(settings); + } + return new VolumeStreamItem(stream, settings); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + if (mNewProfileMode) { + TextView desc = new TextView(getActivity()); + int descPadding = getResources().getDimensionPixelSize( + R.dimen.profile_instruction_padding); + desc.setPadding(descPadding, descPadding, descPadding, descPadding); + desc.setText(R.string.profile_setup_actions_description); + getListView().addHeaderView(desc, null, false); + } + } + + private void updateProfile() { + mProfileManager.updateProfile(mProfile); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + mListView.setAdapter(mAdapter); + getActivity().getActionBar().setTitle(mNewProfileMode + ? R.string.profile_setup_actions_title + : R.string.profile_setup_actions_title_config); + } + + private void requestLockscreenModeDialog() { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + final String[] lockEntries = + getResources().getStringArray(R.array.profile_lockmode_entries); + + int defaultIndex = 0; // no action + for (int i = 0; i < LOCKMODE_MAPPING.length; i++) { + if (LOCKMODE_MAPPING[i] == mProfile.getScreenLockMode()) { + defaultIndex = i; + break; + } + } + + builder.setTitle(R.string.profile_lockmode_title); + builder.setSingleChoiceItems(lockEntries, defaultIndex, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int item) { + mProfile.setScreenLockMode(LOCKMODE_MAPPING[item]); + updateProfile(); + mAdapter.notifyDataSetChanged(); + dialog.dismiss(); + } + }); + + builder.setNegativeButton(android.R.string.cancel, null); + builder.show(); + } + + private void requestAirplaneModeDialog(final AirplaneModeSettings setting) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + final String[] connectionNames = + getResources().getStringArray(R.array.profile_action_generic_connection_entries); + + int defaultIndex = 0; // no action + if (setting.isOverride()) { + if (setting.getValue() == 1) { + defaultIndex = 2; // enabled + } else { + defaultIndex = 1; // disabled + } + } + + builder.setTitle(R.string.profile_airplanemode_title); + builder.setSingleChoiceItems(connectionNames, defaultIndex, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int item) { + switch (item) { + case 0: // disable override + setting.setOverride(false); + break; + case 1: // enable override, disable + setting.setOverride(true); + setting.setValue(0); + break; + case 2: // enable override, enable + setting.setOverride(true); + setting.setValue(1); + break; + } + mProfile.setAirplaneMode(setting); + mAdapter.notifyDataSetChanged(); + updateProfile(); + dialog.dismiss(); + } + }); + + builder.setNegativeButton(android.R.string.cancel, null); + builder.show(); + } + + private void requestProfileRingMode() { + // Launch the ringtone picker + Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); + intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false); + intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); + intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE); + startActivityForResult(intent, RINGTONE_REQUEST_CODE); + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + } + + private void requestRingModeDialog(final RingModeSettings setting) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + final String[] values = getResources().getStringArray(R.array.ring_mode_values); + final String[] names = getResources().getStringArray(R.array.ring_mode_entries); + + int defaultIndex = 0; // normal by default + if (setting.isOverride()) { + if (setting.getValue().equals(values[0] /* normal */)) { + defaultIndex = 0; + } else if (setting.getValue().equals(values[1] /* vibrate */)) { + defaultIndex = 1; // enabled + } else if (setting.getValue().equals(values[2] /* mute */)) { + defaultIndex = 2; // mute + } + } else { + defaultIndex = 3; + } + + builder.setTitle(R.string.ring_mode_title); + builder.setSingleChoiceItems(names, defaultIndex, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int item) { + switch (item) { + case 0: // enable override, normal + setting.setOverride(true); + setting.setValue(values[0]); + break; + case 1: // enable override, vibrate + setting.setOverride(true); + setting.setValue(values[1]); + break; + case 2: // enable override, mute + setting.setOverride(true); + setting.setValue(values[2]); + break; + case 3: + setting.setOverride(false); + break; + } + mProfile.setRingMode(setting); + mAdapter.notifyDataSetChanged(); + updateProfile(); + dialog.dismiss(); + } + }); + + builder.setNegativeButton(android.R.string.cancel, null); + builder.show(); + } + + private void requestConnectionOverrideDialog(final ConnectionSettings setting) { + if (setting == null) { + throw new UnsupportedOperationException("connection setting cannot be null yo"); + } + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + final String[] connectionNames = + getResources().getStringArray(R.array.profile_action_generic_connection_entries); + + int defaultIndex = 0; // no action + if (setting.isOverride()) { + if (setting.getValue() == 1) { + defaultIndex = 2; // enabled + } else { + defaultIndex = 1; // disabled + } + } + + builder.setTitle(ConnectionOverrideItem.getConnectionTitle(setting.getConnectionId())); + builder.setSingleChoiceItems(connectionNames, defaultIndex, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int item) { + switch (item) { + case 0: // disable override + setting.setOverride(false); + break; + case 1: // enable override, disable + setting.setOverride(true); + setting.setValue(0); + break; + case 2: // enable override, enable + setting.setOverride(true); + setting.setValue(1); + break; + } + mProfile.setConnectionSettings(setting); + mAdapter.notifyDataSetChanged(); + updateProfile(); + dialog.dismiss(); + } + }); + + builder.setNegativeButton(android.R.string.cancel, null); + builder.show(); + } + + public void requestVolumeDialog(int streamId, + final StreamSettings streamSettings) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(VolumeStreamItem.getNameForStream(streamId)); + + final AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE); + final LayoutInflater inflater = LayoutInflater.from(getActivity()); + final View view = inflater.inflate(R.layout.dialog_profiles_volume_override, null); + final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekbar); + final CheckBox override = (CheckBox) view.findViewById(R.id.checkbox); + override.setChecked(streamSettings.isOverride()); + override.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + streamSettings.setOverride(isChecked); + seekBar.setEnabled(isChecked); + + mProfile.setStreamSettings(streamSettings); + mAdapter.notifyDataSetChanged(); + updateProfile(); + } + }); + seekBar.setEnabled(streamSettings.isOverride()); + seekBar.setMax(am.getStreamMaxVolume(streamId)); + seekBar.setProgress(streamSettings.getValue()); + builder.setView(view); + builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + int value = seekBar.getProgress(); + streamSettings.setValue(value); + mProfile.setStreamSettings(streamSettings); + mAdapter.notifyDataSetChanged(); + updateProfile(); + dialog.dismiss(); + } + }); + builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + builder.show(); + } + + private void requestProfileName() { + LayoutInflater inflater = LayoutInflater.from(getActivity()); + View dialogView = inflater.inflate(R.layout.profile_name_dialog, null); + + final EditText entry = (EditText) dialogView.findViewById(R.id.name); + entry.setText(mProfile.getName()); + + new AlertDialog.Builder(getActivity()) + .setTitle(R.string.rename_dialog_title) + .setView(dialogView) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String value = entry.getText().toString(); + mProfile.setName(value); + mAdapter.notifyDataSetChanged(); + updateProfile(); + } + }) + .setNegativeButton(android.R.string.cancel, null) + .show(); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_setup_actions, container, false); + + mListView = (ListView) view.findViewById(android.R.id.list); + mListView.setOnItemClickListener(this); + if (mNewProfileMode) { + view.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + getActivity().setResult(Activity.RESULT_CANCELED); + finishFragment(); + } + }); + + view.findViewById(R.id.finish).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + mProfileManager.addProfile(mProfile); + + getActivity().setResult(Activity.RESULT_OK); + finishFragment(); + } + }); + } else { + view.findViewById(R.id.bottom_buttons).setVisibility(View.GONE); + } + return view; + } + + @Override + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + final Item itemAtPosition = (Item) parent.getItemAtPosition(position); + + if (itemAtPosition instanceof AirplaneModeItem) { + AirplaneModeItem item = (AirplaneModeItem) itemAtPosition; + requestAirplaneModeDialog(item.getSettings()); + } else if (itemAtPosition instanceof LockModeItem) { + requestLockscreenModeDialog(); + } else if (itemAtPosition instanceof RingModeItem) { + RingModeItem item = (RingModeItem) itemAtPosition; + requestRingModeDialog(item.getSettings()); + } else if (itemAtPosition instanceof ConnectionOverrideItem) { + ConnectionOverrideItem item = (ConnectionOverrideItem) itemAtPosition; + requestConnectionOverrideDialog(item.getSettings()); + } else if (itemAtPosition instanceof VolumeStreamItem) { + VolumeStreamItem item = (VolumeStreamItem) itemAtPosition; + requestVolumeDialog(item.getStreamType(), item.getSettings()); + } else if (itemAtPosition instanceof ProfileNameItem) { + requestProfileName(); + } + } +} diff --git a/src/com/android/settings/profiles/SetupTriggersFragment.java b/src/com/android/settings/profiles/SetupTriggersFragment.java new file mode 100644 index 0000000..dc88685 --- /dev/null +++ b/src/com/android/settings/profiles/SetupTriggersFragment.java @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2014 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.ActionBar; +import android.app.Activity; +import android.app.Profile; +import android.app.ProfileManager; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Bundle; +import android.support.v4.view.PagerTabStrip; +import android.support.v4.view.ViewPager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import com.android.settings.R; +import com.android.settings.SettingsPreferenceFragment; +import com.android.settings.SubSettings; +import com.android.settings.profiles.triggers.NfcTriggerFragment; + +public class SetupTriggersFragment extends SettingsPreferenceFragment { + + ViewPager mPager; + Profile mProfile; + ProfileManager mProfileManager; + TriggerPagerAdapter mAdapter; + boolean mNewProfileMode; + + private static final int REQUEST_SETUP_ACTIONS = 5; + + public static SetupTriggersFragment newInstance(Profile profile, boolean newProfile) { + SetupTriggersFragment fragment = new SetupTriggersFragment(); + Bundle args = new Bundle(); + args.putParcelable(ProfilesSettings.EXTRA_PROFILE, profile); + args.putBoolean(ProfilesSettings.EXTRA_NEW_PROFILE, newProfile); + + fragment.setArguments(args); + return fragment; + } + + public SetupTriggersFragment() { + // Required empty public constructor + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mProfile = getArguments().getParcelable(ProfilesSettings.EXTRA_PROFILE); + mNewProfileMode = getArguments().getBoolean(ProfilesSettings.EXTRA_NEW_PROFILE, false); + } + mProfileManager = (ProfileManager) getActivity().getSystemService(Context.PROFILE_SERVICE); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + final ActionBar actionBar = getActivity().getActionBar(); + if (mNewProfileMode) { + actionBar.setTitle(R.string.profile_setup_setup_triggers_title); + } else { + String title = getString(R.string.profile_setup_setup_triggers_title_config, + mProfile.getName()); + actionBar.setTitle(title); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View root = inflater.inflate(R.layout.fragment_setup_triggers, container, false); + + ViewPager pager = (ViewPager) root.findViewById(R.id.view_pager); + mAdapter = new TriggerPagerAdapter(getActivity(), getChildFragmentManager()); + + Bundle profileArgs = new Bundle(); + profileArgs.putParcelable(ProfilesSettings.EXTRA_PROFILE, mProfile); + + final TriggerPagerAdapter.TriggerFragments[] fragments = + TriggerPagerAdapter.TriggerFragments.values(); + + for (final TriggerPagerAdapter.TriggerFragments fragment : fragments) { + if (fragment.getFragmentClass() == NfcTriggerFragment.class) { + if (!getActivity().getPackageManager().hasSystemFeature( + PackageManager.FEATURE_NFC)) { + // device doesn't have NFC + continue; + } + } + mAdapter.add(fragment.getFragmentClass(), profileArgs, fragment.getTitleRes()); + } + + pager.setAdapter(mAdapter); + + PagerTabStrip tabs = (PagerTabStrip) root.findViewById(R.id.tabs); + tabs.setTabIndicatorColorResource(R.color.theme_accent); + + if (mNewProfileMode) { + Button nextButton = (Button) root.findViewById(R.id.next); + nextButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Bundle args = new Bundle(); + args.putParcelable(ProfilesSettings.EXTRA_PROFILE, mProfile); + args.putBoolean(ProfilesSettings.EXTRA_NEW_PROFILE, mNewProfileMode); + + SubSettings pa = (SubSettings) getActivity(); + pa.startPreferencePanel(SetupActionsFragment.class.getCanonicalName(), args, + R.string.profile_profile_manage, null, + SetupTriggersFragment.this, REQUEST_SETUP_ACTIONS); + } + }); + + // back button + root.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + finishFragment(); + } + }); + } else { + root.findViewById(R.id.bottom_buttons).setVisibility(View.GONE); + } + return root; + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == REQUEST_SETUP_ACTIONS) { + if (resultCode == Activity.RESULT_OK) { + // exit out of the wizard! + finishFragment(); + } + } + } + + +} diff --git a/src/com/android/settings/profiles/StreamVolumePreference.java b/src/com/android/settings/profiles/StreamVolumePreference.java deleted file mode 100644 index 6788aca..0000000 --- a/src/com/android/settings/profiles/StreamVolumePreference.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * 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.profiles; - -import android.app.AlertDialog; -import android.app.Dialog; -import android.content.Context; -import android.content.DialogInterface; -import android.media.AudioManager; -import android.preference.Preference; -import android.util.AttributeSet; -import android.view.View; -import android.view.ViewGroup; -import android.widget.CheckBox; -import android.widget.CompoundButton; -import android.widget.LinearLayout; -import android.widget.SeekBar; - -import com.android.settings.R; - -public class StreamVolumePreference extends Preference implements - CompoundButton.OnCheckedChangeListener, View.OnClickListener { - - private boolean mProtectFromCheckedChange = false; - - private CheckBox mCheckBox; - - final static String TAG = "StreamVolumePreference"; - - private ProfileConfig.StreamItem mStreamItem; - - private SeekBar mBar; - - /** - * @param context - * @param attrs - * @param defStyle - */ - public StreamVolumePreference(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - init(); - } - - /** - * @param context - * @param attrs - */ - public StreamVolumePreference(Context context, AttributeSet attrs) { - super(context, attrs); - init(); - } - - /** - * @param context - */ - public StreamVolumePreference(Context context) { - super(context); - init(); - } - - @Override - public View getView(View convertView, ViewGroup parent) { - View view = super.getView(convertView, parent); - - View widget = view.findViewById(R.id.profile_checkbox); - if ((widget != null) && widget instanceof CheckBox) { - mCheckBox = (CheckBox) widget; - mCheckBox.setOnCheckedChangeListener(this); - - mProtectFromCheckedChange = true; - mCheckBox.setChecked(isChecked()); - mProtectFromCheckedChange = false; - } - - View textLayout = view.findViewById(R.id.text_layout); - if ((textLayout != null) && textLayout instanceof LinearLayout) { - textLayout.setOnClickListener(this); - } - - return view; - } - - private void init() { - setLayoutResource(R.layout.preference_streamvolume); - } - - public boolean isChecked() { - return mStreamItem != null && mStreamItem.mSettings.isOverride(); - } - - public void setStreamItem(ProfileConfig.StreamItem streamItem) { - mStreamItem = streamItem; - - if (mCheckBox != null) { - mCheckBox.setChecked(mStreamItem.mSettings.isOverride()); - } - } - - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (mProtectFromCheckedChange) { - return; - } - - mStreamItem.mSettings.setOverride(isChecked); - - callChangeListener(isChecked); - } - - protected Dialog createVolumeDialog() { - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - final AudioManager am = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); - builder.setTitle(mStreamItem.mLabel); - mBar = new SeekBar(getContext()); - mBar.setPaddingRelative(32, 16, 32, 16); // TODO: confirm appropriate padding - mBar.setMax(am.getStreamMaxVolume(mStreamItem.mStreamId)); - mBar.setProgress(mStreamItem.mSettings.getValue()); - builder.setView(mBar); - builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - int value = mBar.getProgress(); - mStreamItem.mSettings.setValue(value); - setSummary(getContext().getString(R.string.volume_override_summary) + " " + value + "/" + mBar.getMax()); - } - }); - builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - } - }); - return builder.create(); - } - - public ProfileConfig.StreamItem getStreamItem() { - return mStreamItem; - } - - @Override - public void onClick(android.view.View v) { - if ((v != null) && (R.id.text_layout == v.getId())) { - createVolumeDialog().show(); - } - } -} diff --git a/src/com/android/settings/profiles/TriggerPagerAdapter.java b/src/com/android/settings/profiles/TriggerPagerAdapter.java new file mode 100644 index 0000000..cfd7fff --- /dev/null +++ b/src/com/android/settings/profiles/TriggerPagerAdapter.java @@ -0,0 +1,219 @@ +/* + * Copyright (C) 2014 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.Activity; +import android.app.Fragment; +import android.app.FragmentManager; +import android.os.Bundle; +import android.support.v13.app.FragmentPagerAdapter; +import android.util.SparseArray; +import android.view.ViewGroup; + +import com.android.settings.R; +import com.android.settings.profiles.triggers.BluetoothTriggerFragment; +import com.android.settings.profiles.triggers.NfcTriggerFragment; +import com.android.settings.profiles.triggers.WifiTriggerFragment; +import com.google.android.collect.Lists; + +import java.lang.ref.WeakReference; +import java.util.List; + +/** + * A {@link android.support.v4.app.FragmentPagerAdapter} class for swiping between playlists, recent, + * artists, albums, songs, and genre {@link android.support.v4.app.Fragment}s on phones.<br/> + */ +public class TriggerPagerAdapter extends FragmentPagerAdapter { + + private final SparseArray<WeakReference<Fragment>> mFragmentArray = + new SparseArray<WeakReference<Fragment>>(); + + private final List<Holder> mHolderList = Lists.newArrayList(); + + private final Activity mFragmentActivity; + + private int mCurrentPage; + + /** + * Constructor of <code>PagerAdatper<code> + * + * @param activity The {@link android.support.v4.app.FragmentActivity} of the + * {@link android.support.v4.app.Fragment}. + * @param fm the FragmentManager to use. + */ + public TriggerPagerAdapter(Activity activity, FragmentManager fm) { + super(fm); + mFragmentActivity = activity; + } + + /** + * Method that adds a new fragment class to the viewer (the fragment is + * internally instantiate) + * + * @param className The full qualified name of fragment class. + * @param params The instantiate params. + */ + @SuppressWarnings("synthetic-access") + public void add(final Class<? extends Fragment> className, final Bundle params, + final int titleResId) { + final Holder mHolder = new Holder(); + mHolder.mClassName = className.getName(); + mHolder.mParams = params; + mHolder.mTitleResId = titleResId; + + final int mPosition = mHolderList.size(); + mHolderList.add(mPosition, mHolder); + notifyDataSetChanged(); + } + + /** + * Method that returns the {@link android.support.v4.app.Fragment} in the argument + * position. + * + * @param position The position of the fragment to return. + * @return Fragment The {@link android.support.v4.app.Fragment} in the argument position. + */ + public Fragment getFragment(final int position) { + final WeakReference<Fragment> mWeakFragment = mFragmentArray.get(position); + if (mWeakFragment != null && mWeakFragment.get() != null) { + return mWeakFragment.get(); + } + return getItem(position); + } + + /** + * {@inheritDoc} + */ + @Override + public Object instantiateItem(final ViewGroup container, final int position) { + final Fragment mFragment = (Fragment)super.instantiateItem(container, position); + final WeakReference<Fragment> mWeakFragment = mFragmentArray.get(position); + if (mWeakFragment != null) { + mWeakFragment.clear(); + } + mFragmentArray.put(position, new WeakReference<Fragment>(mFragment)); + return mFragment; + } + + /** + * {@inheritDoc} + */ + @Override + public Fragment getItem(final int position) { + final Holder mCurrentHolder = mHolderList.get(position); + final Fragment mFragment = Fragment.instantiate(mFragmentActivity, + mCurrentHolder.mClassName, mCurrentHolder.mParams); + return mFragment; + } + + /** + * {@inheritDoc} + */ + @Override + public void destroyItem(final ViewGroup container, final int position, final Object object) { + super.destroyItem(container, position, object); + final WeakReference<Fragment> mWeakFragment = mFragmentArray.get(position); + if (mWeakFragment != null) { + mWeakFragment.clear(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public int getCount() { + return mHolderList.size(); + } + + /** + * {@inheritDoc} + */ + @Override + public CharSequence getPageTitle(final int position) { + return mFragmentActivity.getString(mHolderList.get(position).mTitleResId); + } + + /** + * Method that returns the current page position. + * + * @return int The current page. + */ + public int getCurrentPage() { + return mCurrentPage; + } + + /** + * Method that sets the current page position. + * + * @param currentPage The current page. + */ + protected void setCurrentPage(final int currentPage) { + mCurrentPage = currentPage; + } + + /** + * An enumeration of all the main fragments supported. + */ + public enum TriggerFragments { + /** + * The artist fragment + */ + WIFI(WifiTriggerFragment.class, R.string.profile_tabs_wifi), + /** + * The album fragment + */ + BLUETOOTH(BluetoothTriggerFragment.class, R.string.profile_tabs_bluetooth), + /** + * The song fragment + */ + NFC(NfcTriggerFragment.class, R.string.profile_tabs_nfc); + + private Class<? extends Fragment> mFragmentClass; + private int mNameRes; + + /** + * Constructor of <code>MusicFragments</code> + * + * @param fragmentClass The fragment class + */ + private TriggerFragments(final Class<? extends Fragment> fragmentClass, int nameRes) { + mFragmentClass = fragmentClass; + mNameRes = nameRes; + } + + /** + * Method that returns the fragment class. + * + * @return Class<? extends Fragment> The fragment class. + */ + public Class<? extends Fragment> getFragmentClass() { + return mFragmentClass; + } + + public int getTitleRes() { return mNameRes; } + } + + /** + * A private class with information about fragment initialization + */ + private final static class Holder { + String mClassName; + int mTitleResId; + Bundle mParams; + } +} diff --git a/src/com/android/settings/profiles/TriggersFragment.java b/src/com/android/settings/profiles/TriggersFragment.java deleted file mode 100644 index 5da8186..0000000 --- a/src/com/android/settings/profiles/TriggersFragment.java +++ /dev/null @@ -1,229 +0,0 @@ -/* - * 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.ActionBar; -import android.app.AlertDialog; -import android.app.Dialog; -import android.app.Profile; -import android.app.Profile.ProfileTrigger; -import android.app.Profile.TriggerType; -import android.app.ProfileManager; -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; -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 android.util.Log; -import android.widget.ArrayAdapter; - -import com.android.settings.R; -import com.android.settings.SettingsPreferenceFragment; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.Set; - -/** - * Settings Preference to configure triggers to switch profiles base on Wi-Fi events - */ -public class TriggersFragment extends SettingsPreferenceFragment implements ActionBar.OnNavigationListener { - private Profile mProfile; - private Preference mSelectedTrigger; - private ProfileManager mProfileManager; - private WifiManager mWifiManager; - private BluetoothAdapter mBluetoothAdapter; - - private int mTriggerFilter = 0; - private static final int WIFI_TRIGGER = 1; - private static final int BT_TRIGGER = 2; - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - mProfileManager = (ProfileManager) getSystemService(Context.PROFILE_SERVICE); - mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); - mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();; - addPreferencesFromResource(R.xml.wifi_settings); - } - - @Override - public void onResume() { - super.onResume(); - loadPreferences(); - loadActionBar(); - } - - private void loadActionBar() { - final ActionBar actionBar = getActivity().getActionBar(); - actionBar.setDisplayShowTitleEnabled(true); - actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); - Resources res = getResources(); - - String[] navItems = res.getStringArray(R.array.profile_trigger_filters); - ArrayAdapter<String> navAdapter = new ArrayAdapter<String>( - actionBar.getThemedContext(), android.R.layout.simple_list_item_1, navItems); - - // Set up the dropdown list navigation in the action bar. - actionBar.setListNavigationCallbacks(navAdapter, this); - actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP - | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE); - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - Bundle args = getArguments(); - if (args != null) { - mProfile = args.getParcelable("profile"); - } - } - - private void initPreference(AbstractTriggerPreference pref, int state, Resources res, int icon) { - String summary = res.getStringArray(R.array.profile_trigger_wifi_options)[state]; - pref.setSummary(summary); - pref.setTriggerState(state); - pref.setIcon(icon); - } - - private void loadPreferences() { - final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); - final Resources res = getResources(); - final List<AbstractTriggerPreference> prefs = new ArrayList<AbstractTriggerPreference>(); - - getPreferenceScreen().removeAll(); - - if (mTriggerFilter == WIFI_TRIGGER || mTriggerFilter == 0) { - if (configs != null ) { - for (WifiConfiguration config : configs) { - WifiTriggerAPPreference accessPoint = - new WifiTriggerAPPreference(getActivity(), config); - int state = mProfile.getTrigger(Profile.TriggerType.WIFI, accessPoint.getSSID()); - initPreference(accessPoint, state, res, R.drawable.ic_wifi_signal_4_dark); - prefs.add(accessPoint); - } - } else { - final List<ProfileTrigger> triggers = mProfile.getTriggersFromType(TriggerType.WIFI); - for (ProfileTrigger trigger : triggers) { - WifiTriggerAPPreference accessPoint = - new WifiTriggerAPPreference(getActivity(), trigger.getName()); - initPreference(accessPoint, trigger.getState(), res, R.drawable.ic_wifi_signal_4_dark); - prefs.add(accessPoint); - } - } - } - - Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); - if (mTriggerFilter == BT_TRIGGER || mTriggerFilter == 0) { - if (!pairedDevices.isEmpty()) { - for (BluetoothDevice device : pairedDevices) { - BluetoothTriggerPreference bt = - new BluetoothTriggerPreference(getActivity(), device); - int state = mProfile.getTrigger(Profile.TriggerType.BLUETOOTH, bt.getAddress()); - initPreference(bt, state, res, R.drawable.ic_settings_bluetooth2); - prefs.add(bt); - } - } else { - final List<ProfileTrigger> triggers = mProfile.getTriggersFromType(TriggerType.BLUETOOTH); - for (ProfileTrigger trigger : triggers) { - BluetoothTriggerPreference bt = new BluetoothTriggerPreference(getActivity(), - trigger.getName(), trigger.getId()); - initPreference(bt, trigger.getState(), res, R.drawable.ic_settings_bluetooth2); - prefs.add(bt); - } - } - } - - Collections.sort(prefs, new Comparator<AbstractTriggerPreference>() { - @Override - public int compare(AbstractTriggerPreference o1, AbstractTriggerPreference o2) { - if (o1.getTriggerState() == o2.getTriggerState()) { - return o1.compareTo(o2); - } - return o1.getTriggerState() < o2.getTriggerState() ? -1 : 1; - } - }); - for (Preference pref: prefs) { - getPreferenceScreen().addPreference(pref); - } - - } - - @Override - public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { - mSelectedTrigger = preference; - if (preference instanceof WifiTriggerAPPreference) { - showDialog(WIFI_TRIGGER); - return true; - } else if (preference instanceof BluetoothTriggerPreference) { - showDialog(BT_TRIGGER); - return true; - } - return super.onPreferenceTreeClick(screen, preference); - } - - @Override - public Dialog onCreateDialog(final int dialogId) { - final String id; - final String triggerName = mSelectedTrigger.getTitle().toString(); - final int triggerType; - - switch (dialogId) { - case WIFI_TRIGGER: - WifiTriggerAPPreference pref = (WifiTriggerAPPreference) mSelectedTrigger; - id = pref.getSSID(); - triggerType = Profile.TriggerType.WIFI; - break; - case BT_TRIGGER: - BluetoothTriggerPreference btpref = (BluetoothTriggerPreference) mSelectedTrigger; - id = btpref.getAddress(); - triggerType = Profile.TriggerType.BLUETOOTH; - break; - default: - return super.onCreateDialog(dialogId); - } - - return new AlertDialog.Builder(getActivity()) - .setTitle(R.string.profile_trigger_configure) - .setSingleChoiceItems(R.array.profile_trigger_wifi_options, - mProfile.getTrigger(triggerType, id), - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - mProfile.setTrigger(triggerType, id, which, triggerName); - mProfileManager.updateProfile(mProfile); - loadPreferences(); - dialog.dismiss(); - } - }) - .create(); - } - - @Override - public boolean onNavigationItemSelected(int itemPosition, long itemId) { - mTriggerFilter = itemPosition; - loadPreferences(); - return true; - } -} diff --git a/src/com/android/settings/profiles/WifiTriggerAPPreference.java b/src/com/android/settings/profiles/WifiTriggerAPPreference.java deleted file mode 100644 index 239509a..0000000 --- a/src/com/android/settings/profiles/WifiTriggerAPPreference.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.content.Context; -import android.net.wifi.WifiConfiguration; - -public class WifiTriggerAPPreference extends AbstractTriggerPreference { - - private String mSSID; - private WifiConfiguration mConfig; - - WifiTriggerAPPreference(Context context, WifiConfiguration config) { - super(context); - loadConfig(config); - setTitle(mSSID); - } - - WifiTriggerAPPreference(Context context, String ssid) { - super(context); - mSSID = ssid; - setTitle(mSSID); - } - - private void loadConfig(WifiConfiguration config) { - mSSID = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID)); - mConfig = config; - } - - 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/actions/ItemListAdapter.java b/src/com/android/settings/profiles/actions/ItemListAdapter.java new file mode 100644 index 0000000..d09e0ee --- /dev/null +++ b/src/com/android/settings/profiles/actions/ItemListAdapter.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2014 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.actions; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import com.android.settings.profiles.actions.item.Item; + +import java.util.List; + +public class ItemListAdapter extends ArrayAdapter<Item> { + private LayoutInflater mInflater; + + public enum RowType { + HEADER_ITEM, + CONNECTION_ITEM, + VOLUME_STREAM_ITEM, + NAME_ITEM, + RINGMODE_ITEM, + AIRPLANEMODE_ITEM, + LOCKSCREENMODE_ITEM + } + + public ItemListAdapter(Context context, List<Item> items) { + super(context, 0, items); + mInflater = LayoutInflater.from(context); + } + + @Override + public int getViewTypeCount() { + return RowType.values().length; + + } + + @Override + public int getItemViewType(int position) { + return getItem(position).getRowType().ordinal(); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + return getItem(position).getView(mInflater, convertView, parent); + } + + @Override + public boolean areAllItemsEnabled() { + return false; + } + + @Override + public boolean isEnabled(int position) { + return getItem(position).isEnabled(); + } +} diff --git a/src/com/android/settings/profiles/actions/item/AirplaneModeItem.java b/src/com/android/settings/profiles/actions/item/AirplaneModeItem.java new file mode 100644 index 0000000..ed589d5 --- /dev/null +++ b/src/com/android/settings/profiles/actions/item/AirplaneModeItem.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2014 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.actions.item; + +import android.app.AirplaneModeSettings; +import android.app.RingModeSettings; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import com.android.settings.R; +import com.android.settings.profiles.actions.ItemListAdapter; + +public class AirplaneModeItem implements Item { + AirplaneModeSettings mSettings; + + public AirplaneModeItem(AirplaneModeSettings airplaneModeSettings) { + if (airplaneModeSettings == null) { + airplaneModeSettings = new AirplaneModeSettings(); + } + mSettings = airplaneModeSettings; + } + + @Override + public ItemListAdapter.RowType getRowType() { + return ItemListAdapter.RowType.AIRPLANEMODE_ITEM; + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public View getView(LayoutInflater inflater, View convertView, ViewGroup parent) { + View view; + if (convertView == null) { + view = inflater.inflate(R.layout.list_two_line_item, parent, false); + // Do some initialization + } else { + view = convertView; + } + + TextView text = (TextView) view.findViewById(R.id.title); + text.setText(R.string.profile_airplanemode_title); + + TextView desc = (TextView) view.findViewById(R.id.summary); + desc.setText(getModeString(mSettings)); + + return view; + } + + public AirplaneModeSettings getSettings() { + return mSettings; + } + + public static int getModeString(AirplaneModeSettings settings) { + if (settings.isOverride()) { + if (settings.getValue() == 1) { + return R.string.profile_action_enable; + } else { + return R.string.profile_action_disable; + } + } else { + return R.string.profile_action_none; + } + } +} diff --git a/src/com/android/settings/profiles/actions/item/ConnectionOverrideItem.java b/src/com/android/settings/profiles/actions/item/ConnectionOverrideItem.java new file mode 100644 index 0000000..74efd27 --- /dev/null +++ b/src/com/android/settings/profiles/actions/item/ConnectionOverrideItem.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2014 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.actions.item; + +import android.app.ConnectionSettings; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.android.settings.R; +import com.android.settings.profiles.actions.ItemListAdapter; + +public class ConnectionOverrideItem implements Item { + int mConnectionId; + ConnectionSettings mConnectionSettings; + + public ConnectionOverrideItem(int connectionId, ConnectionSettings settings) { + mConnectionId = connectionId; + if (settings == null) { + settings = new ConnectionSettings(connectionId); + } + this.mConnectionSettings = settings; + } + + @Override + public ItemListAdapter.RowType getRowType() { + return ItemListAdapter.RowType.CONNECTION_ITEM; + } + + @Override + public View getView(LayoutInflater inflater, View convertView, ViewGroup parent) { + View view; + if (convertView == null) { + view = inflater.inflate(R.layout.list_two_line_item, parent, false); + // Do some initialization + } else { + view = convertView; + } + + TextView title = (TextView) view.findViewById(R.id.title); + TextView summary = (TextView) view.findViewById(R.id.summary); + + title.setText(getConnectionTitle(mConnectionId)); + summary.setText(getSummary()); + + return view; + } + + @Override + public boolean isEnabled() { + return true; + } + + public static int getConnectionTitle(int connectionId) { + switch (connectionId) { + case ConnectionSettings.PROFILE_CONNECTION_BLUETOOTH: + return R.string.toggleBluetooth; + case ConnectionSettings.PROFILE_CONNECTION_MOBILEDATA: + return R.string.toggleData; + case ConnectionSettings.PROFILE_CONNECTION_2G3G: + return R.string.toggle2g3g; + case ConnectionSettings.PROFILE_CONNECTION_GPS: + return R.string.toggleGPS; + case ConnectionSettings.PROFILE_CONNECTION_NFC: + return R.string.toggleNfc; + case ConnectionSettings.PROFILE_CONNECTION_SYNC: + return R.string.toggleSync; + case ConnectionSettings.PROFILE_CONNECTION_WIFI: + return R.string.toggleWifi; + case ConnectionSettings.PROFILE_CONNECTION_WIFIAP: + return R.string.toggleWifiAp; + default: + return 0; + } + } + + public int getSummary() { + if (mConnectionSettings != null && mConnectionSettings.isOverride()) { + if (mConnectionSettings.getValue() == 1) { + return R.string.profile_action_enable; + } else { + return R.string.profile_action_disable; + } + } else { + return R.string.profile_action_none; + } + } + + public ConnectionSettings getSettings() { + return mConnectionSettings; + } + + public int getConnectionType() { + return mConnectionId; + } +} diff --git a/src/com/android/settings/profiles/actions/item/Header.java b/src/com/android/settings/profiles/actions/item/Header.java new file mode 100644 index 0000000..7d08508 --- /dev/null +++ b/src/com/android/settings/profiles/actions/item/Header.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2014 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.actions.item; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.android.settings.R; +import com.android.settings.profiles.actions.ItemListAdapter; + +public class Header implements Item { + private final String name; + + public Header(String name) { + this.name = name; + } + + @Override + public ItemListAdapter.RowType getRowType() { + return ItemListAdapter.RowType.HEADER_ITEM; + } + + @Override + public View getView(LayoutInflater inflater, View convertView, ViewGroup parent) { + View view; + if (convertView == null) { + view = inflater.inflate(R.layout.profile_list_header, parent, false); + // Do some initialization + } else { + view = convertView; + } + + TextView text = (TextView) view.findViewById(R.id.title); + text.setText(name); + + return view; + } + + @Override + public boolean isEnabled() { + return false; + } +} diff --git a/src/com/android/settings/profiles/actions/item/Item.java b/src/com/android/settings/profiles/actions/item/Item.java new file mode 100644 index 0000000..c468e11 --- /dev/null +++ b/src/com/android/settings/profiles/actions/item/Item.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2014 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.actions.item; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.android.settings.profiles.actions.ItemListAdapter; + +public interface Item { + public ItemListAdapter.RowType getRowType(); + public View getView(LayoutInflater inflater, View convertView, ViewGroup parent); + public boolean isEnabled(); +} diff --git a/src/com/android/settings/profiles/actions/item/LockModeItem.java b/src/com/android/settings/profiles/actions/item/LockModeItem.java new file mode 100644 index 0000000..e9e93b5 --- /dev/null +++ b/src/com/android/settings/profiles/actions/item/LockModeItem.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2014 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.actions.item; + +import android.app.Profile; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.android.settings.R; +import com.android.settings.profiles.actions.ItemListAdapter; + +public class LockModeItem implements Item { + Profile mProfile; + + public LockModeItem(Profile profile) { + mProfile = profile; + } + + @Override + public ItemListAdapter.RowType getRowType() { + return ItemListAdapter.RowType.LOCKSCREENMODE_ITEM; + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public View getView(LayoutInflater inflater, View convertView, ViewGroup parent) { + View view; + if (convertView == null) { + view = inflater.inflate(R.layout.list_two_line_item, parent, false); + // Do some initialization + } else { + view = convertView; + } + + TextView text = (TextView) view.findViewById(R.id.title); + text.setText(R.string.profile_lockmode_title); + + TextView desc = (TextView) view.findViewById(R.id.summary); + desc.setText(getSummaryString(mProfile)); + + return view; + } + + public static int getSummaryString(Profile profile) { + switch (profile.getScreenLockMode()) { + case Profile.LockMode.DEFAULT: + return R.string.profile_lockmode_default_summary; + case Profile.LockMode.DISABLE: + return R.string.profile_lockmode_disabled_summary; + case Profile.LockMode.INSECURE: + return R.string.profile_lockmode_insecure_summary; + default: return 0; + } + } +} diff --git a/src/com/android/settings/profiles/actions/item/ProfileNameItem.java b/src/com/android/settings/profiles/actions/item/ProfileNameItem.java new file mode 100644 index 0000000..a31ac32 --- /dev/null +++ b/src/com/android/settings/profiles/actions/item/ProfileNameItem.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2014 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.actions.item; + +import android.app.Profile; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.android.settings.R; +import com.android.settings.profiles.actions.ItemListAdapter; + +public class ProfileNameItem implements Item { + Profile mProfile; + + public ProfileNameItem(Profile profile) { + this.mProfile = profile; + } + + @Override + public ItemListAdapter.RowType getRowType() { + return ItemListAdapter.RowType.NAME_ITEM; + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public View getView(LayoutInflater inflater, View convertView, ViewGroup parent) { + View view; + if (convertView == null) { + view = inflater.inflate(R.layout.profile_list_item, parent, false); + // Do some initialization + } else { + view = convertView; + } + + TextView text = (TextView) view.findViewById(R.id.title); + text.setText(mProfile.getName()); + + return view; + } +} diff --git a/src/com/android/settings/profiles/actions/item/RingModeItem.java b/src/com/android/settings/profiles/actions/item/RingModeItem.java new file mode 100644 index 0000000..1874909 --- /dev/null +++ b/src/com/android/settings/profiles/actions/item/RingModeItem.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2014 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.actions.item; + +import android.app.AlertDialog; +import android.app.RingModeSettings; +import android.content.Context; +import android.content.DialogInterface; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.android.settings.R; +import com.android.settings.profiles.actions.ItemListAdapter; + +public class RingModeItem implements Item { + RingModeSettings mSettings; + + public RingModeItem(RingModeSettings ringModeSettings) { + if (ringModeSettings == null) { + ringModeSettings = new RingModeSettings(); + } + mSettings = ringModeSettings; + } + + @Override + public ItemListAdapter.RowType getRowType() { + return ItemListAdapter.RowType.RINGMODE_ITEM; + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public View getView(LayoutInflater inflater, View convertView, ViewGroup parent) { + View view; + if (convertView == null) { + view = inflater.inflate(R.layout.list_two_line_item, parent, false); + // Do some initialization + } else { + view = convertView; + } + + TextView text = (TextView) view.findViewById(R.id.title); + text.setText(R.string.ring_mode_title); + + TextView desc = (TextView) view.findViewById(R.id.summary); + desc.setText(getModeString(mSettings)); + + return view; + } + + public static int getModeString(RingModeSettings settings) { + if (settings == null) { + return R.string.ring_mode_normal; + } + if (settings.isOverride()) { + if (settings.getValue().equals("vibrate")) { + return R.string.ring_mode_vibrate; + } else if (settings.getValue().equals("normal")) { + return R.string.ring_mode_normal; + } else { + return R.string.ring_mode_mute; + } + } else { + return R.string.ring_mode_unchanged; + } + } + + public RingModeSettings getSettings() { + return mSettings; + } +} diff --git a/src/com/android/settings/profiles/actions/item/VolumeStreamItem.java b/src/com/android/settings/profiles/actions/item/VolumeStreamItem.java new file mode 100644 index 0000000..25db5e0 --- /dev/null +++ b/src/com/android/settings/profiles/actions/item/VolumeStreamItem.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2014 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.actions.item; + +import android.app.StreamSettings; +import android.content.Context; +import android.media.AudioManager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.android.settings.R; +import com.android.settings.profiles.actions.ItemListAdapter; + +public class VolumeStreamItem implements Item { + private int mStreamId; + private StreamSettings mStreamSettings; + + public VolumeStreamItem(int streamId, StreamSettings streamSettings) { + mStreamId = streamId; + mStreamSettings = streamSettings; + } + + @Override + public ItemListAdapter.RowType getRowType() { + return ItemListAdapter.RowType.VOLUME_STREAM_ITEM; + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public View getView(LayoutInflater inflater, View convertView, ViewGroup parent) { + View view; + if (convertView == null) { + view = inflater.inflate(R.layout.list_two_line_item, parent, false); + // Do some initialization + } else { + view = convertView; + } + + Context context = inflater.getContext(); + final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + + TextView text = (TextView) view.findViewById(R.id.title); + text.setText(getNameForStream(mStreamId)); + + TextView desc = (TextView) view.findViewById(R.id.summary); + int denominator = mStreamSettings.getValue(); + int numerator = am.getStreamMaxVolume(mStreamId); + if (mStreamSettings.isOverride()) { + desc.setText(context.getResources().getString(R.string.volume_override_summary, + denominator, numerator)); + } else { + desc.setText(context.getString(R.string.volume_override_summary_no_override)); + } + + return view; + } + + public static int getNameForStream(int stream) { + switch (stream) { + case AudioManager.STREAM_ALARM: + return R.string.alarm_volume_title; + case AudioManager.STREAM_MUSIC: + return R.string.media_volume_title; + case AudioManager.STREAM_RING: + return R.string.incoming_call_volume_title; + case AudioManager.STREAM_NOTIFICATION: + return R.string.notification_volume_title; + default: return 0; + } + } + + public int getStreamType() { + return mStreamId; + } + + public StreamSettings getSettings() { + return mStreamSettings; + } +} diff --git a/src/com/android/settings/profiles/AbstractTriggerPreference.java b/src/com/android/settings/profiles/triggers/AbstractTriggerItem.java index 1bc2ec0..b028490 100644 --- a/src/com/android/settings/profiles/AbstractTriggerPreference.java +++ b/src/com/android/settings/profiles/triggers/AbstractTriggerItem.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 The CyanogenMod Project + * Copyright (C) 2014 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. @@ -13,18 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -package com.android.settings.profiles; +package com.android.settings.profiles.triggers; import android.app.Profile; -import android.content.Context; -import android.preference.Preference; -public class AbstractTriggerPreference extends Preference { - public AbstractTriggerPreference(Context context) { - super(context); - } +public class AbstractTriggerItem { + private int mIcon; + private String mSummary; + private String mTitle; private int mTriggerState = Profile.TriggerState.DISABLED; @@ -35,4 +32,28 @@ public class AbstractTriggerPreference extends Preference { public int getTriggerState() { return mTriggerState; } + + public void setSummary(String summary) { + mSummary = summary; + } + + public String getTitle() { + return mTitle; + } + + public void setTitle(String title) { + mTitle = title; + } + + public String getSummary() { + return mSummary; + } + + public void setIcon(int icon) { + mIcon = icon; + } + + public int getIcon() { + return mIcon; + } } diff --git a/src/com/android/settings/profiles/triggers/BluetoothTriggerFragment.java b/src/com/android/settings/profiles/triggers/BluetoothTriggerFragment.java new file mode 100644 index 0000000..d7fa67e --- /dev/null +++ b/src/com/android/settings/profiles/triggers/BluetoothTriggerFragment.java @@ -0,0 +1,248 @@ +/* + * Copyright (C) 2014 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.triggers; + +import android.app.AlertDialog; +import android.app.ListFragment; +import android.app.Profile; +import android.app.ProfileManager; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothClass; +import android.bluetooth.BluetoothDevice; +import android.content.Context; +import android.content.DialogInterface; +import android.content.res.Resources; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.TextView; +import com.android.settings.R; +import com.android.settings.profiles.ProfilesSettings; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + + +public class BluetoothTriggerFragment extends ListFragment { + + private BluetoothAdapter mBluetoothAdapter; + + Profile mProfile; + ProfileManager mProfileManager; + + private List<BluetoothTrigger> mTriggers = new ArrayList<BluetoothTrigger>(); + private BluetoothTriggerAdapter mListAdapter; + + public static BluetoothTriggerFragment newInstance(Profile profile) { + BluetoothTriggerFragment fragment = new BluetoothTriggerFragment(); + + Bundle extras = new Bundle(); + extras.putParcelable(ProfilesSettings.EXTRA_PROFILE, profile); + + fragment.setArguments(extras); + return fragment; + } + + public BluetoothTriggerFragment() { + // Required empty public constructor + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mProfileManager = (ProfileManager) getActivity().getSystemService(Context.PROFILE_SERVICE); + if (getArguments() != null) { + mProfile = getArguments().getParcelable(ProfilesSettings.EXTRA_PROFILE); + } + mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + } + + @Override + public void onResume() { + super.onResume(); + reloadTriggerListItems(); + } + + private void initPreference(AbstractTriggerItem pref, int state, Resources res, int icon) { + String[] values = res.getStringArray(R.array.profile_trigger_wifi_options_values); + for (int i = 0; i < values.length; i++) { + if (Integer.parseInt(values[i]) == state) { + pref.setSummary(res.getStringArray(R.array.profile_trigger_wifi_options)[i]); + break; + } + } + pref.setTriggerState(state); + pref.setIcon(icon); + } + + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + super.onListItemClick(l, v, position, id); + + final String triggerId; + final String triggerName; + final int triggerType; + + String[] entries = getResources().getStringArray(R.array.profile_trigger_wifi_options); + String[] values = + getResources().getStringArray(R.array.profile_trigger_wifi_options_values); + + List<Trigger> triggers = new ArrayList<Trigger>(entries.length); + for (int i = 0; i < entries.length; i++) { + Trigger toAdd = new Trigger(); + toAdd.value = Integer.parseInt(values[i]); + toAdd.name = entries[i]; + triggers.add(toAdd); + } + + BluetoothTrigger btpref = mListAdapter.getItem(position); + triggerName = btpref.getTitle(); + triggerId = btpref.getAddress(); + triggerType = Profile.TriggerType.BLUETOOTH; + BluetoothDevice dev = mBluetoothAdapter.getRemoteDevice(triggerId); + if (!dev.getBluetoothClass().doesClassMatch(BluetoothClass.PROFILE_A2DP)) { + removeTrigger(triggers, Profile.TriggerState.ON_A2DP_CONNECT); + removeTrigger(triggers, Profile.TriggerState.ON_A2DP_DISCONNECT); + } + + entries = new String[triggers.size()]; + final int[] valueInts = new int[triggers.size()]; + int currentTrigger = mProfile.getTrigger(triggerType, triggerId); + int currentItem = -1; + for (int i = 0; i < triggers.size(); i++) { + Trigger t = triggers.get(i); + entries[i] = t.name; + valueInts[i] = t.value; + if (valueInts[i] == currentTrigger) { + currentItem = i; + } + } + + new AlertDialog.Builder(getActivity()) + .setTitle(R.string.profile_trigger_configure) + .setSingleChoiceItems(entries, currentItem, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + mProfile.setTrigger(triggerType, triggerId, valueInts[which], triggerName); + mProfileManager.updateProfile(mProfile); + reloadTriggerListItems(); + dialog.dismiss(); + } + }) + .show(); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + reloadTriggerListItems(); + mListAdapter = new BluetoothTriggerAdapter(getActivity()); + setListAdapter(mListAdapter); + setEmptyText(getString(R.string.no_bluetooth_triggers)); + } + + private void removeTrigger(List<Trigger> triggers, int value) { + for (Trigger t : triggers) { + if (t.value == value) { + triggers.remove(t); + return; + } + } + } + + private void reloadTriggerListItems() { + mTriggers.clear(); + final Resources res = getResources(); + + Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); + if (!pairedDevices.isEmpty()) { + for (BluetoothDevice device : pairedDevices) { + BluetoothTrigger bt = + new BluetoothTrigger(device); + int state = mProfile.getTrigger(Profile.TriggerType.BLUETOOTH, bt.getAddress()); + initPreference(bt, state, res, R.drawable.ic_settings_bluetooth); + mTriggers.add(bt); + } + } else { + final List<Profile.ProfileTrigger> triggers = + mProfile.getTriggersFromType(Profile.TriggerType.BLUETOOTH); + for (Profile.ProfileTrigger trigger : triggers) { + BluetoothTrigger bt = new BluetoothTrigger(trigger.getName(), trigger.getId()); + initPreference(bt, trigger.getState(), res, R.drawable.ic_settings_bluetooth); + mTriggers.add(bt); + } + } + + if (mListAdapter != null) { + mListAdapter.notifyDataSetChanged(); + } + } + + private class Trigger { + int value; + String name; + } + + private class BluetoothTriggerAdapter extends ArrayAdapter<BluetoothTrigger> { + public BluetoothTriggerAdapter(Context context) { + super(context, R.layout.abstract_trigger_row, R.id.title, mTriggers); + } + + @Override + public View getView(int i, View view, ViewGroup viewGroup) { + LayoutInflater inflater = LayoutInflater.from(getContext()); + View rowView = inflater.inflate(R.layout.abstract_trigger_row, viewGroup, false); + TextView title = (TextView) rowView.findViewById(R.id.title); + TextView desc = (TextView) rowView.findViewById(R.id.desc); + ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); + + BluetoothTrigger trigger = getItem(i); + + title.setText(trigger.getTitle()); + desc.setText(trigger.getSummary()); + imageView.setImageResource(trigger.getIcon()); + + return rowView; + } + } + + public static class BluetoothTrigger extends AbstractTriggerItem { + private String mAddress; + + public BluetoothTrigger(BluetoothDevice device) { + mAddress = device.getAddress(); + if (device.getAlias() != null) { + setTitle(device.getAlias()); + } else { + setTitle(device.getName()); + } + } + + public BluetoothTrigger(String name, String address) { + mAddress = address; + setTitle(name); + } + + public String getAddress() { + return mAddress; + } + } +} diff --git a/src/com/android/settings/profiles/triggers/NfcTriggerFragment.java b/src/com/android/settings/profiles/triggers/NfcTriggerFragment.java new file mode 100644 index 0000000..86194ad --- /dev/null +++ b/src/com/android/settings/profiles/triggers/NfcTriggerFragment.java @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2014 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.triggers; + +import android.app.Fragment; +import android.app.PendingIntent; +import android.app.Profile; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.nfc.NfcAdapter; +import android.nfc.Tag; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Toast; +import com.android.settings.R; +import com.android.settings.Settings; +import com.android.settings.SubSettings; +import com.android.settings.profiles.NFCProfileTagCallback; +import com.android.settings.profiles.NFCProfileUtils; +import com.android.settings.profiles.ProfilesSettings; + + +public class NfcTriggerFragment extends Fragment implements NFCProfileTagCallback { + Profile mProfile; + + private NfcAdapter mNfcAdapter; + private IntentFilter[] mWriteTagFilters; + + public static NfcTriggerFragment newInstance(Profile profile) { + NfcTriggerFragment fragment = new NfcTriggerFragment(); + + Bundle extras = new Bundle(); + extras.putParcelable(ProfilesSettings.EXTRA_PROFILE, profile); + + fragment.setArguments(extras); + return fragment; + } + + public NfcTriggerFragment() { + // Required empty public constructor + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity()); + if (getArguments() != null) { + mProfile = getArguments().getParcelable(ProfilesSettings.EXTRA_PROFILE); + } + ((SubSettings) getActivity()).setNfcProfileCallback(this); + } + + @Override + public void onDestroy() { + super.onDestroy(); + ((SubSettings) getActivity()).setNfcProfileCallback(null); + } + + @Override + public void onResume() { + super.onResume(); + if (mProfile != null) { + enableTagWriteMode(); + } + } + + @Override + public void onPause() { + super.onPause(); + disableTagWriteMode(); + } + + private PendingIntent getPendingIntent() { + Intent intent = new Intent(getActivity(), getActivity().getClass()) + .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + return PendingIntent.getActivity(getActivity(), 0, intent, 0); + } + + private void disableTagWriteMode() { + mNfcAdapter.disableForegroundDispatch(getActivity()); + } + + private void enableTagWriteMode() { + IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); + mWriteTagFilters = new IntentFilter[] { + tagDetected + }; + mNfcAdapter.enableForegroundDispatch(getActivity(), getPendingIntent(), mWriteTagFilters, null); + } + + @Override + public void onTagRead(Tag tag) { + if (NFCProfileUtils.writeTag(NFCProfileUtils.getProfileAsNdef(mProfile), tag)) { + Toast.makeText(getActivity(), R.string.profile_write_success, Toast.LENGTH_LONG).show(); + NFCProfileUtils.vibrate(getActivity()); + } else { + Toast.makeText(getActivity(), R.string.profile_write_failed, Toast.LENGTH_LONG).show(); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + return inflater.inflate(R.layout.nfc_writer, container, false); + } + + +} diff --git a/src/com/android/settings/profiles/triggers/WifiTriggerFragment.java b/src/com/android/settings/profiles/triggers/WifiTriggerFragment.java new file mode 100644 index 0000000..5ba28bf --- /dev/null +++ b/src/com/android/settings/profiles/triggers/WifiTriggerFragment.java @@ -0,0 +1,258 @@ +/* + * Copyright (C) 2014 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.triggers; + +import android.app.AlertDialog; +import android.app.ListFragment; +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.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.BaseAdapter; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.TextView; +import com.android.settings.R; +import com.android.settings.profiles.ProfilesSettings; + +import java.util.ArrayList; +import java.util.List; + +public class WifiTriggerFragment extends ListFragment { + WifiManager mWifiManager; + Profile mProfile; + private ProfileManager mProfileManager; + + private List<WifiTrigger> mTriggers = new ArrayList<WifiTrigger>(); + private WifiTriggerAdapter mListAdapter; + + public static WifiTriggerFragment newInstance(Profile profile) { + WifiTriggerFragment fragment = new WifiTriggerFragment(); + + Bundle extras = new Bundle(); + extras.putParcelable(ProfilesSettings.EXTRA_PROFILE, profile); + + fragment.setArguments(extras); + return fragment; + } + + public WifiTriggerFragment() { + // Required empty public constructor + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mProfile = getArguments().getParcelable(ProfilesSettings.EXTRA_PROFILE); + } else { + throw new UnsupportedOperationException("no profile!"); + } + mProfileManager = (ProfileManager) getActivity().getSystemService(Context.PROFILE_SERVICE); + mWifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE); + } + + @Override + public void onResume() { + super.onResume(); + reloadTriggerListItems(); + } + + private void initPreference(AbstractTriggerItem pref, int state, Resources res, int icon) { + String[] values = res.getStringArray(R.array.profile_trigger_wifi_options_values); + for (int i = 0; i < values.length; i++) { + if (Integer.parseInt(values[i]) == state) { + pref.setSummary(res.getStringArray(R.array.profile_trigger_wifi_options)[i]); + break; + } + } + pref.setTriggerState(state); + pref.setIcon(icon); + } + + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + super.onListItemClick(l, v, position, id); + + final String triggerId; + final String triggerName; + final int triggerType; + + String[] entries = getResources().getStringArray(R.array.profile_trigger_wifi_options); + String[] values = + getResources().getStringArray(R.array.profile_trigger_wifi_options_values); + + List<Trigger> triggers = new ArrayList<Trigger>(entries.length); + for (int i = 0; i < entries.length; i++) { + Trigger toAdd = new Trigger(); + toAdd.value = Integer.parseInt(values[i]); + toAdd.name = entries[i]; + triggers.add(toAdd); + } + + WifiTrigger pref = (WifiTrigger) l.getAdapter().getItem(position); + triggerName = pref.getTitle(); + triggerId = pref.getSSID(); + triggerType = Profile.TriggerType.WIFI; + removeTrigger(triggers, Profile.TriggerState.ON_A2DP_CONNECT); + removeTrigger(triggers, Profile.TriggerState.ON_A2DP_DISCONNECT); + + entries = new String[triggers.size()]; + final int[] valueInts = new int[triggers.size()]; + int currentTrigger = mProfile.getTrigger(triggerType, triggerId); + int currentItem = -1; + for (int i = 0; i < triggers.size(); i++) { + Trigger t = triggers.get(i); + entries[i] = t.name; + valueInts[i] = t.value; + if (valueInts[i] == currentTrigger) { + currentItem = i; + } + } + + new AlertDialog.Builder(getActivity()) + .setTitle(R.string.profile_trigger_configure) + .setSingleChoiceItems(entries, currentItem, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + mProfile.setTrigger(triggerType, triggerId, valueInts[which], triggerName); + mProfileManager.updateProfile(mProfile); + reloadTriggerListItems(); + dialog.dismiss(); + } + }) + .show(); + } + + private void removeTrigger(List<Trigger> triggers, int value) { + for (Trigger t : triggers) { + if (t.value == value) { + triggers.remove(t); + return; + } + } + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + reloadTriggerListItems(); + mListAdapter = new WifiTriggerAdapter(getActivity()); + setListAdapter(mListAdapter); + setEmptyText(getString(R.string.no_wifi_triggers)); + } + + private void reloadTriggerListItems() { + mTriggers.clear(); + final Resources res = getResources(); + final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); + + if (configs != null) { + for (WifiConfiguration config : configs) { + WifiTrigger accessPoint = new WifiTrigger(config); + int state = mProfile.getTrigger(Profile.TriggerType.WIFI, accessPoint.getSSID()); + initPreference(accessPoint, state, res, R.drawable.ic_wifi_signal_4_dark); + mTriggers.add(accessPoint); + } + } else { + final List<Profile.ProfileTrigger> triggers = + mProfile.getTriggersFromType(Profile.TriggerType.WIFI); + for (Profile.ProfileTrigger trigger : triggers) { + WifiTrigger accessPoint = new WifiTrigger(trigger.getName()); + initPreference(accessPoint, trigger.getState(), res, + R.drawable.ic_wifi_signal_4_dark); + mTriggers.add(accessPoint); + } + } + if (mListAdapter != null) { + mListAdapter.notifyDataSetChanged(); + } + } + + private class Trigger { + int value; + String name; + } + + private class WifiTriggerAdapter extends ArrayAdapter<WifiTrigger> { + public WifiTriggerAdapter(Context context) { + super(context, R.layout.abstract_trigger_row, R.id.title, mTriggers); + } + + @Override + public View getView(int i, View view, ViewGroup viewGroup) { + LayoutInflater inflater = LayoutInflater.from(getContext()); + View rowView = inflater.inflate(R.layout.abstract_trigger_row, viewGroup, false); + TextView title = (TextView) rowView.findViewById(R.id.title); + TextView desc = (TextView) rowView.findViewById(R.id.desc); + ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); + + WifiTrigger trigger = getItem(i); + + title.setText(trigger.getTitle()); + desc.setText(trigger.getSummary()); + imageView.setImageResource(trigger.getIcon()); + + return rowView; + } + } + + public static class WifiTrigger extends AbstractTriggerItem { + public String mSSID; + public WifiConfiguration mConfig; + + public WifiTrigger(WifiConfiguration config) { + mConfig = config; + loadConfig(config); + } + + public WifiTrigger(String ssid) { + mSSID = ssid; + } + + public String getSSID() { + return mSSID; + } + + @Override + public String getTitle() { + return mSSID; + } + + private void loadConfig(WifiConfiguration config) { + mSSID = (config.SSID == null ? "" : removeDoubleQuotes(config.SSID)); + mConfig = config; + } + + 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; + } + } +} |