Add intent for enabling phone account.

Bug: 20303449
Change-Id: If6e717260df01160308fd395a6aac413aa5206d5
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index ac3799f..051660e 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -228,6 +228,19 @@
             </intent-filter>
         </activity>
 
+        <activity-alias android:name="ProtectedEnableAccountPreferenceActivity"
+                  android:targetActivity=".settings.EnableAccountPreferenceActivity"
+                  android:label="@string/enable_account_preference_title"
+                  android:configChanges="orientation|screenSize|keyboardHidden"
+                  android:theme="@style/Theme.Telecom.DialerSettings"
+                  android:process=":ui"
+                  android:permission="android.permission.MODIFY_PHONE_STATE">
+            <intent-filter>
+                <action android:name="android.telecom.action.ENABLE_PHONE_ACCOUNT_SETTING" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity-alias>
+
         <activity android:name=".components.ErrorDialogActivity"
                 android:configChanges="orientation|screenSize|keyboardHidden"
                 android:excludeFromRecents="true"
diff --git a/src/com/android/server/telecom/PhoneAccountRegistrar.java b/src/com/android/server/telecom/PhoneAccountRegistrar.java
index 1ef869b..3d539cb 100644
--- a/src/com/android/server/telecom/PhoneAccountRegistrar.java
+++ b/src/com/android/server/telecom/PhoneAccountRegistrar.java
@@ -350,12 +350,16 @@
 
     public void enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
         PhoneAccount account = getPhoneAccount(accountHandle);
-        if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
+        if (account == null) {
+            Log.w(this, "Could not find account to enable: " + accountHandle);
+            return;
+        } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
             // We never change the enabled state of SIM-based accounts.
+            Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
             return;
         }
 
-        if (account != null && account.isEnabled() != isEnabled) {
+        if (account.isEnabled() != isEnabled) {
             account.setIsEnabled(isEnabled);
             write();
             fireAccountsChanged();
diff --git a/src/com/android/server/telecom/settings/EnableAccountPreferenceActivity.java b/src/com/android/server/telecom/settings/EnableAccountPreferenceActivity.java
index 383b416..728100c 100644
--- a/src/com/android/server/telecom/settings/EnableAccountPreferenceActivity.java
+++ b/src/com/android/server/telecom/settings/EnableAccountPreferenceActivity.java
@@ -18,7 +18,12 @@
 
 import android.app.ActionBar;
 import android.app.Activity;
+import android.content.ComponentName;
+import android.content.Intent;
 import android.os.Bundle;
+import android.telecom.Log;
+import android.telecom.PhoneAccountHandle;
+import android.telecom.TelecomManager;
 import android.view.MenuItem;
 
 public class EnableAccountPreferenceActivity extends Activity {
@@ -26,6 +31,12 @@
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
+
+        if (handleDirectChangeRequest()) {
+            finish();
+            return;
+        }
+
         getFragmentManager().beginTransaction()
                 .replace(android.R.id.content, new EnableAccountPreferenceFragment())
                 .commit();
@@ -36,6 +47,54 @@
         }
     }
 
+    private boolean handleDirectChangeRequest() {
+        // Here we check to see if the intent action is the protected version which allows
+        // for immediate enabling/disabling of phone accounts. If it is, take the phone account
+        // handle and value and simply enable/disable the account. If any part is missing, then
+        // open the setting screen as normal.
+        Intent intent = getIntent();
+        String action = intent.getAction();
+        if (!TelecomManager.ACTION_ENABLE_PHONE_ACCOUNT_SETTING.equals(action)) {
+            return false;
+        }
+
+        if (!intent.hasExtra(TelecomManager.EXTRA_ENABLE_PHONE_ACCOUNT_VALUE)) {
+            Log.w(this, "Boolean extra value not found in %s",
+                    TelecomManager.EXTRA_ENABLE_PHONE_ACCOUNT_VALUE);
+            return false;
+        }
+
+        String desc = intent.getStringExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_DESCRIPTION);
+        if (desc == null) {
+            Log.w(this, "Extra value not found or is null in %s",
+                    TelecomManager.EXTRA_PHONE_ACCOUNT_DESCRIPTION);
+            return false;
+        }
+
+        String[] parts = desc.split(";");
+        if (parts.length < 2) {
+            Log.w(this, "Description not split into two parts with semi-colon: %s", desc);
+            return false;
+        }
+
+        ComponentName component = ComponentName.unflattenFromString(parts[0]);
+        if (component == null) {
+            Log.w(this, "Value does not unflatten to ComponentName: %s", parts[0]);
+            return false;
+        }
+
+        PhoneAccountHandle handle = new PhoneAccountHandle(component, parts[1]);
+        boolean enabled = intent.getBooleanExtra(
+                TelecomManager.EXTRA_ENABLE_PHONE_ACCOUNT_VALUE, false);
+        try {
+            TelecomManager.from(this).enablePhoneAccount(handle, enabled);
+            return true;
+        } catch (Exception e) {
+            Log.e(this, e, "Exception enabling account %s, %s", parts[0], parts[1]);
+            return false;
+        }
+    }
+
     /** ${inheritDoc} */
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {