diff options
4 files changed, 180 insertions, 147 deletions
diff --git a/res/layout/default_sim_checkbox.xml b/res/layout/default_sim_checkbox.xml deleted file mode 100644 index a0c4a9a0..00000000 --- a/res/layout/default_sim_checkbox.xml +++ /dev/null @@ -1,23 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" - android:id="@+id/default_sim_checkbox_layout" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_marginTop="8dip" - android:orientation="vertical"> - <!-- Dummy to enable right-justification of checkbox --> - <TextView - android:layout_width="wrap_content" - android:layout_height="wrap_content"/> - <CheckBox - android:id="@+id/default_sim_checkbox_view" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:paddingStart="20dip" - android:layout_marginLeft="20dip" - android:layout_marginBottom="20dip" - android:gravity="center" - android:textSize="14sp" - android:textAlignment="viewStart" - android:text="@string/set_default_sim"/> -</LinearLayout> diff --git a/src/com/android/contacts/common/SubscriptionManager.java b/src/com/android/contacts/common/SubscriptionManager.java new file mode 100644 index 00000000..95c0e0d0 --- /dev/null +++ b/src/com/android/contacts/common/SubscriptionManager.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2014 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. + * 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.contacts.common; + +import android.telecomm.Subscription; +import android.telephony.TelephonyManager; + +import java.util.List; + +/** + * To pass current subscription information between activities/fragments. + */ +public class SubscriptionManager { + private Subscription mCurrentSubscription = null; + private TelephonyManager mTelephonyManager; + + public SubscriptionManager(TelephonyManager telephonyManager, Subscription subscription) { + mTelephonyManager = telephonyManager; + mCurrentSubscription = subscription; + } + + public SubscriptionManager(TelephonyManager telephonyManager) { + mTelephonyManager = telephonyManager; + } + + public Subscription getCurrentSubscription() { + return mCurrentSubscription; + } + + public void setCurrentSubscription(Subscription subscription) { + mCurrentSubscription = subscription; + } + + public List<Subscription> getSubscriptions() { + return mTelephonyManager.getSubscriptions(); + } +} diff --git a/src/com/android/contacts/common/dialog/SelectSIMDialogFragment.java b/src/com/android/contacts/common/dialog/SelectSIMDialogFragment.java deleted file mode 100644 index e15aa203..00000000 --- a/src/com/android/contacts/common/dialog/SelectSIMDialogFragment.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.android.contacts.common.dialog; - -import android.app.Activity; -import android.app.AlertDialog; -import android.app.Dialog; -import android.app.DialogFragment; -import android.app.FragmentManager; -import android.content.DialogInterface; -import android.os.Bundle; -import android.util.Log; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.ViewGroup; -import android.view.ViewGroup.LayoutParams; -import android.widget.CheckBox; -import android.widget.CompoundButton; -import android.widget.LinearLayout; -import android.widget.ListView; -import android.widget.TextView; - -import com.android.contacts.common.R; - -/** - * Dialog that allows the user to switch between default SIM cards - */ -public class SelectSIMDialogFragment extends DialogFragment { - private int mInitialSelectedItem; - private int mCurrentSelectedItem; - private boolean mHasDefaultSim; - private boolean mIsDefaultSet; - private OnClickOkListener mOkListener; - - /* Preferred way to show this dialog */ - public static void show(FragmentManager fragmentManager, - int currentSimCard) { - SelectSIMDialogFragment fragment = new SelectSIMDialogFragment(); - fragment.mInitialSelectedItem = currentSimCard; - fragment.mCurrentSelectedItem = currentSimCard; - fragment.show(fragmentManager, "selectSIM"); - } - - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - // TODO: Get the sim names programmatically - CharSequence[] sims = {"SIM1-ATT", "SIM2-TMobile"}; - - // TODO: pull default sim from database - mHasDefaultSim = false; - - final DialogInterface.OnClickListener selectionListener = - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - mCurrentSelectedItem = which; - } - }; - final DialogInterface.OnClickListener okListener = - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int id) { - boolean isChanged = mInitialSelectedItem != mCurrentSelectedItem; - - if (isChanged) { - mOkListener.passSimUpdate(mCurrentSelectedItem); - } - if (mIsDefaultSet && (!mHasDefaultSim || isChanged)) { - // if setting a new default, save to database - // TODO: save to database - } - else if (!mIsDefaultSet && mHasDefaultSim) { - // if unchecking the checkbox, remove the default - // TODO: remove from database - } - } - }; - - final CompoundButton.OnCheckedChangeListener checkListener = - new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton check, boolean isChecked) { - mIsDefaultSet = isChecked; - } - }; - - // Generate custom checkbox view - LinearLayout checkboxLayout = (LinearLayout) getActivity() - .getLayoutInflater() - .inflate(R.layout.default_sim_checkbox, null); - - CheckBox cb = (CheckBox) checkboxLayout.findViewById(R.id.default_sim_checkbox_view); - cb.setOnCheckedChangeListener(checkListener); - cb.setChecked(mHasDefaultSim); - - AlertDialog dialog = new AlertDialog.Builder(getActivity()) - .setTitle(R.string.menu_select_sim) - .setSingleChoiceItems(sims, mInitialSelectedItem, selectionListener) - .setPositiveButton(android.R.string.ok, okListener) - .setNegativeButton(android.R.string.cancel, null) - .create(); - - dialog.getListView().addFooterView(checkboxLayout); - - return dialog; - } - - /* - * Interface to help pass updated SIM information to the main dialer - */ - public interface OnClickOkListener { - public void passSimUpdate(int simId); - } - - @Override - public void onAttach(Activity activity) { - super.onAttach(activity); - - try { - mOkListener = (OnClickOkListener) activity; - } catch (ClassCastException e) { - throw new ClassCastException(activity.toString() - + " must implement OnClickOkListener"); - } - } -}
\ No newline at end of file diff --git a/src/com/android/contacts/common/dialog/SelectSubscriptionDialogFragment.java b/src/com/android/contacts/common/dialog/SelectSubscriptionDialogFragment.java new file mode 100644 index 00000000..f874de09 --- /dev/null +++ b/src/com/android/contacts/common/dialog/SelectSubscriptionDialogFragment.java @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2014 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. + * 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.contacts.common.dialog; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.DialogFragment; +import android.app.FragmentManager; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.telecomm.Subscription; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.view.ViewGroup.LayoutParams; +import android.widget.ArrayAdapter; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.LinearLayout; +import android.widget.ListAdapter; +import android.widget.ListView; +import android.widget.TextView; + +import com.android.contacts.common.R; +import com.android.contacts.common.SubscriptionManager; +import com.android.internal.util.ArrayUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * Dialog that allows the user to switch between default SIM cards + */ +public class SelectSubscriptionDialogFragment extends DialogFragment { + private SubscriptionManager mSubscriptionManager; + private int mSelection; + private List<Subscription> mSubscriptions; + private static final int NO_SELECTION = -1; + + /* Preferred way to show this dialog */ + public static void show(FragmentManager fragmentManager, + SubscriptionManager subscriptionManager) { + SelectSubscriptionDialogFragment fragment = new SelectSubscriptionDialogFragment(); + fragment.mSubscriptionManager = subscriptionManager; + fragment.show(fragmentManager, "selectSubscription"); + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + mSubscriptions = mSubscriptionManager.getSubscriptions(); + mSelection = NO_SELECTION; + + final DialogInterface.OnClickListener selectionListener = + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + mSelection = which; + } + }; + final DialogInterface.OnClickListener okListener = + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + if (mSelection != NO_SELECTION) { + // No need to update the current subscription if it has not been changed + mSubscriptionManager.setCurrentSubscription(mSubscriptions.get(mSelection)); + } + } + }; + + CharSequence[] names = getSubscriptionNames(); + return new AlertDialog.Builder(getActivity()) + .setTitle(R.string.menu_select_sim) + .setSingleChoiceItems(names, getSelectedSubscriptionIndex(names), selectionListener) + .setPositiveButton(android.R.string.ok, okListener) + .setNegativeButton(android.R.string.cancel, null) + .create(); + } + + /** + * Helper function to extract the index of the currently selected subscription. + * Used in the dialog as the initially selected radio button. + * + * @param activeSubs String list of the labels referring to each of possible + * active subscriptions to choose from + * @return the index of the selected subscription in the array of subscriptions + */ + private int getSelectedSubscriptionIndex(CharSequence[] activeSubs) { + Subscription initialSubscription = mSubscriptionManager.getCurrentSubscription(); + if (initialSubscription == null) { + return -1; + } + else { + return ArrayUtils.indexOf(activeSubs, initialSubscription.getLabel(getActivity())); + } + } + + /** + * Extracts the label names from each of the subscriptions and returns as a list of strings + * + * @return a list of strings to display in the dialog + */ + private CharSequence[] getSubscriptionNames() { + Context context = getActivity(); + List<String> strings = new ArrayList<String>(); + for (int i = 0; i < mSubscriptions.size(); i++) { + strings.add(mSubscriptions.get(i).getLabel(context)); + } + return strings.toArray(new CharSequence[strings.size()]); + } +}
\ No newline at end of file |