OSDN Git Service

Settings: Add a shortcut icon for carrier selection
authorMichael W <baddaemon87@gmail.com>
Fri, 11 Aug 2017 17:05:27 +0000 (19:05 +0200)
committerLuca Stefani <luca020400@lineageos.org>
Sun, 13 Aug 2017 18:54:56 +0000 (18:54 +0000)
* This is a re-implementation from cm-13.0
* The new implementation pop ups a SIM chooser in case of
  multisim and opens the network operator selection for the
  selected sim rather than the first one
* In case of single-sim it directly opens the network operator
  selection for the sim

Change-Id: Iad7f43b3e56449432fe009bf44d68d703768981e

AndroidManifest.xml
res/values/cm_strings.xml
src/com/android/settings/CarrierSelection.java [new file with mode: 0644]

index 82a7fb5..d604274 100755 (executable)
             </intent-filter>
         </activity>
 
+        <activity android:name=".CarrierSelection"
+            android:label="@string/shortcut_carrier_title"
+            android:theme="@android:style/Theme.DeviceDefault.Light.Dialog.NoActionBar"
+            android:excludeFromRecents="true">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="com.android.settings.SHORTCUT" />
+            </intent-filter>
+        </activity>
+
     </application>
 </manifest>
index 844796e..4bbc476 100644 (file)
     <!-- IC Codes -->
     <string name="ic_code_model">Model: %1$s</string>
     <string name="ic_code_full">IC: %1$s</string>
+
+    <!-- Label for settings shortcut: carrier selection -->
+    <string name="shortcut_carrier_title">Network operators</string>
 </resources>
diff --git a/src/com/android/settings/CarrierSelection.java b/src/com/android/settings/CarrierSelection.java
new file mode 100644 (file)
index 0000000..ffb3279
--- /dev/null
@@ -0,0 +1,185 @@
+/*
+ * Copyright (C) 2014-2016 The CyanogenMod Project
+ * Copyright (C) 2017 The LineageOS Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.view.KeyEvent;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.ImageView;
+import android.widget.ListAdapter;
+import android.widget.TextView;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class CarrierSelection extends Activity {
+    public static final String EXTRA_SUB_ID = "sub_id";
+    public static final int INVALID_SUB_ID = -1000;
+
+    @Override
+    public void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+
+        final SubscriptionManager subscriptionManager = SubscriptionManager.from(this);
+        final List<SubscriptionInfo> subInfoList =
+                subscriptionManager.getActiveSubscriptionInfoList();
+        if (subInfoList == null) {
+            startNetworkSelection(INVALID_SUB_ID);
+        } else if (subInfoList.size() == 1) {
+            startNetworkSelection(subInfoList.get(0).getSubscriptionId());
+        } else {
+            createDialog(this, subInfoList).show();
+        }
+    }
+
+    public Dialog createDialog(final Context context, final List<SubscriptionInfo> subInfoList) {
+        final ArrayList<String> list = new ArrayList<String>();
+        final int selectableSubInfoLength = subInfoList == null ? 0 : subInfoList.size();
+
+        final DialogInterface.OnClickListener selectionListener =
+                new DialogInterface.OnClickListener() {
+                    @Override
+                    public void onClick(DialogInterface dialog, int value) {
+                        final SubscriptionInfo sir = subInfoList.get(value);
+                        startNetworkSelection(sir.getSubscriptionId());
+                    }
+                };
+
+        Dialog.OnKeyListener keyListener = new Dialog.OnKeyListener() {
+            @Override
+            public boolean onKey(DialogInterface arg0, int keyCode,
+                                 KeyEvent event) {
+                if (keyCode == KeyEvent.KEYCODE_BACK) {
+                    finish();
+                }
+                return true;
+            }
+        };
+
+        for (int i = 0; i < selectableSubInfoLength; ++i) {
+            final SubscriptionInfo sir = subInfoList.get(i);
+            CharSequence displayName = sir.getDisplayName();
+            if (displayName == null) {
+                displayName = "";
+            }
+            list.add(displayName.toString());
+        }
+
+        String[] arr = list.toArray(new String[0]);
+
+        AlertDialog.Builder builder = new AlertDialog.Builder(context);
+
+        ListAdapter adapter = new CarrierSelection.SelectAccountListAdapter(
+                subInfoList,
+                builder.getContext(),
+                R.layout.select_account_list_item,
+                arr);
+        builder.setTitle(R.string.sim_select_card);
+
+        Dialog dialog = builder.setAdapter(adapter, selectionListener).create();
+        dialog.setOnKeyListener(keyListener);
+
+        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
+            @Override
+            public void onCancel(DialogInterface dialogInterface) {
+                finish();
+            }
+        });
+
+        return dialog;
+    }
+
+    private void startNetworkSelection(int subId) {
+        Intent intent = new Intent(Intent.ACTION_MAIN);
+        intent.setComponent(new ComponentName("com.android.phone",
+                "com.android.phone.NetworkSetting"));
+        if (subId != INVALID_SUB_ID) {
+            intent.putExtra(EXTRA_SUB_ID, subId);
+        }
+        startActivity(intent);
+        finish();
+    }
+
+    private class SelectAccountListAdapter extends ArrayAdapter<String> {
+        private Context mContext;
+        private int mResId;
+        private final float OPACITY = 0.54f;
+        private List<SubscriptionInfo> mSubInfoList;
+
+        public SelectAccountListAdapter(List<SubscriptionInfo> subInfoList,
+                                        Context context, int resource, String[] arr) {
+            super(context, resource, arr);
+            mContext = context;
+            mResId = resource;
+            mSubInfoList = subInfoList;
+        }
+
+        @Override
+        public View getView(int position, View convertView, ViewGroup parent) {
+            LayoutInflater inflater = (LayoutInflater)
+                    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+            View rowView;
+            final CarrierSelection.SelectAccountListAdapter.ViewHolder holder;
+
+            if (convertView == null) {
+                // Cache views for faster scrolling
+                rowView = inflater.inflate(mResId, null);
+                holder = new CarrierSelection.SelectAccountListAdapter.ViewHolder();
+                holder.title = (TextView) rowView.findViewById(R.id.title);
+                holder.summary = (TextView) rowView.findViewById(R.id.summary);
+                holder.icon = (ImageView) rowView.findViewById(R.id.icon);
+                rowView.setTag(holder);
+            } else {
+                rowView = convertView;
+                holder = (CarrierSelection.SelectAccountListAdapter.ViewHolder) rowView.getTag();
+            }
+
+            final SubscriptionInfo sir = mSubInfoList.get(position);
+            if (sir == null) {
+                holder.title.setText(getItem(position));
+                holder.summary.setText("");
+                holder.icon.setImageDrawable(getResources()
+                        .getDrawable(R.drawable.ic_live_help));
+                holder.icon.setAlpha(OPACITY);
+            } else {
+                holder.title.setText(sir.getDisplayName());
+                holder.summary.setText(sir.getNumber());
+                holder.icon.setImageBitmap(sir.createIconBitmap(mContext));
+            }
+            return rowView;
+        }
+
+        private class ViewHolder {
+            TextView title;
+            TextView summary;
+            ImageView icon;
+        }
+    }
+}