Don't crash settings when cast is disabled

Also clean up warnings

b/21451355

Change-Id: Ibd369dde7516e1804ce2cbf7359eb3149ea5ad8a
diff --git a/Settings/src/com/android/tv/settings/BrowseInfo.java b/Settings/src/com/android/tv/settings/BrowseInfo.java
index bb22041..637bcb5 100644
--- a/Settings/src/com/android/tv/settings/BrowseInfo.java
+++ b/Settings/src/com/android/tv/settings/BrowseInfo.java
@@ -50,7 +50,6 @@
 import com.android.tv.settings.accessories.AccessoryUtils;
 import com.android.tv.settings.accessories.BluetoothAccessoryActivity;
 import com.android.tv.settings.accessories.BluetoothConnectionsManager;
-import com.android.tv.settings.accounts.AccountImageUriGetter;
 import com.android.tv.settings.accounts.AccountSettingsActivity;
 import com.android.tv.settings.accounts.AddAccountWithTypeActivity;
 import com.android.tv.settings.accounts.AuthenticatorHelper;
@@ -60,7 +59,6 @@
 import com.android.tv.settings.device.sound.SoundActivity;
 import com.android.tv.settings.users.RestrictedProfileDialogFragment;
 import com.android.tv.settings.util.UriUtils;
-import com.android.tv.settings.util.AccountImageHelper;
 
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
@@ -74,13 +72,9 @@
  */
 public class BrowseInfo extends BrowseInfoBase {
 
-    private static final String TAG = "CanvasSettings.BrowseInfo";
+    private static final String TAG = "TvSettings.BrowseInfo";
     private static final boolean DEBUG = false;
 
-    public static final String EXTRA_ACCESSORY_ADDRESS = "accessory_address";
-    public static final String EXTRA_ACCESSORY_NAME = "accessory_name";
-    public static final String EXTRA_ACCESSORY_ICON_ID = "accessory_icon_res";
-
     private static final String ACCOUNT_TYPE_GOOGLE = "com.google";
 
     private static final String ETHERNET_PREFERENCE_KEY = "ethernet";
@@ -155,9 +149,7 @@
                     }
                 }
 
-            } catch (XmlPullParserException e) {
-                throw new RuntimeException("Error parsing headers", e);
-            } catch (IOException e) {
+            } catch (XmlPullParserException|IOException e) {
                 throw new RuntimeException("Error parsing headers", e);
             } finally {
                 if (parser != null)
@@ -167,11 +159,11 @@
     }
 
     private static final String PREF_KEY_ADD_ACCOUNT = "add_account";
-    private static final String PREF_KEY_ADD_ACCESSORY = "add_accessory";
     private static final String PREF_KEY_WIFI = "network";
     private static final String PREF_KEY_DEVELOPER = "developer";
     private static final String PREF_KEY_INPUTS = "inputs";
     private static final String PREF_KEY_HOME = "home";
+    private static final String PREF_KEY_CAST = "cast";
 
     private final Context mContext;
     private final AuthenticatorHelper mAuthenticatorHelper;
@@ -281,7 +273,7 @@
                 addAccounts(mRow);
             } else if (PREF_KEY_HOME.equals(key)) {
                 // Only show home screen setting if there's a system app to handle the intent.
-                Intent recIntent = getIntent(parser, attrs, mHeaderId);
+                Intent recIntent = getIntent(parser, attrs);
                 if (systemIntentIsHandled(recIntent)) {
                     mRow.add(new MenuItem.Builder()
                             .id(mNextItemId++)
@@ -290,12 +282,22 @@
                             .intent(recIntent)
                             .build());
                 }
-            } else if (!key.equals(PREF_KEY_INPUTS) || mInputSettingNeeded) {
+            } else if (PREF_KEY_CAST.equals(key)) {
+                Intent i = getIntent(parser, attrs);
+                if (systemIntentIsHandled(i)) {
+                    mRow.add(new MenuItem.Builder()
+                            .id(mNextItemId++)
+                            .title(title)
+                            .imageResourceId(mContext, iconRes)
+                            .intent(i)
+                            .build());
+                }
+            } else if (!PREF_KEY_INPUTS.equals(key) || mInputSettingNeeded) {
                 MenuItem.TextGetter descriptionGetter = getDescriptionTextGetterFromKey(key);
                 MenuItem.UriGetter uriGetter = getIconUriGetterFromKey(key);
                 MenuItem.Builder builder = new MenuItem.Builder().id(mNextItemId++).title(title)
                         .descriptionGetter(descriptionGetter)
-                        .intent(getIntent(parser, attrs, mHeaderId));
+                        .intent(getIntent(parser, attrs));
                 if(uriGetter == null) {
                     builder.imageResourceId(mContext, iconRes);
                 } else {
@@ -395,14 +397,14 @@
         ComponentName componentName = new ComponentName("com.android.tv.settings",
                 "com.android.tv.settings.accessories.AddAccessoryActivity");
         Intent i = new Intent().setComponent(componentName);
-        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
+        i.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
         row.add(new MenuItem.Builder().id(mNextItemId++)
                 .title(mContext.getString(R.string.accessories_add))
                 .imageResourceId(mContext, R.drawable.ic_settings_bluetooth)
                 .intent(i).build());
     }
 
-    private Intent getIntent(XmlResourceParser parser, AttributeSet attrs, int headerId)
+    private Intent getIntent(XmlResourceParser parser, AttributeSet attrs)
             throws org.xmlpull.v1.XmlPullParserException, IOException {
         Intent intent = null;
         if (parser.next() == XmlPullParser.START_TAG && "intent".equals(parser.getName())) {
@@ -424,7 +426,9 @@
 
             XmlUtils.skipCurrentTag(parser);
         }
-        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
+        if (intent != null) {
+            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
+        }
         return intent;
     }
 
@@ -474,7 +478,7 @@
         int googleAccountCount = 0;
 
         for (AuthenticatorDescription authDesc : authTypes) {
-            Resources resources = null;
+            final Resources resources;
             try {
                 resources = pm.getResourcesForApplication(authDesc.packageName);
             } catch (NameNotFoundException e) {
@@ -526,7 +530,7 @@
             for (final Account account : accounts) {
                 Intent i = new Intent(mContext, AccountSettingsActivity.class)
                         .putExtra(AccountSettingsActivity.EXTRA_ACCOUNT, account.name);
-                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
+                i.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
                 row.add(new MenuItem.Builder().id(mNextItemId++)
                         .title(authTitle != null ? authTitle : account.name)
                         .imageUri(imageUri)
@@ -548,7 +552,7 @@
             if (!allowableAccountTypes.isEmpty()) {
                 Intent i = new Intent().setComponent(new ComponentName("com.android.tv.settings",
                         "com.android.tv.settings.accounts.AddAccountWithTypeActivity"));
-                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
+                i.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
                 i.putExtra(AddAccountWithTypeActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
                         allowableAccountTypes.toArray(new String[allowableAccountTypes.size()]));
 
@@ -579,7 +583,7 @@
                 int resourceId = AccessoryUtils.getImageIdForDevice(device);
                 Intent i = BluetoothAccessoryActivity.getIntent(mContext, device.getAddress(),
                         device.getName(), resourceId);
-                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
+                i.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
 
                 String desc = connectedBluetoothAddresses.contains(device.getAddress())
                         ? mContext.getString(R.string.accessory_connected)
diff --git a/Settings/src/com/android/tv/settings/MenuItemPresenter.java b/Settings/src/com/android/tv/settings/MenuItemPresenter.java
index 1666d37..aa01c33 100644
--- a/Settings/src/com/android/tv/settings/MenuItemPresenter.java
+++ b/Settings/src/com/android/tv/settings/MenuItemPresenter.java
@@ -17,13 +17,14 @@
 package com.android.tv.settings;
 
 import android.animation.ObjectAnimator;
-import android.app.Activity;
+import android.content.ActivityNotFoundException;
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.support.v17.leanback.widget.Presenter;
 import android.text.TextUtils;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -37,6 +38,8 @@
 
 public class MenuItemPresenter extends Presenter {
 
+    private static final String TAG = "MenuItemPresenter";
+
     private static class MenuItemViewHolder extends ViewHolder {
         public final ImageView mIconView;
         public final TextView mTitleView;
@@ -89,7 +92,11 @@
                 @Override
                 public void onClick(View v) {
                     if (v != null && menuItem.getIntent() != null) {
-                        ((Activity) v.getContext()).startActivity(menuItem.getIntent());
+                        try {
+                            v.getContext().startActivity(menuItem.getIntent());
+                        } catch (ActivityNotFoundException e) {
+                            Log.e(TAG, "Activity not found", e);
+                        }
                     }
                 }
             });