Merge "Import translations. DO NOT MERGE ANYWHERE" into sc-qpr1-dev
diff --git a/src/com/android/settings/applications/appinfo/AppButtonsPreferenceController.java b/src/com/android/settings/applications/appinfo/AppButtonsPreferenceController.java
index 09bf86a..1b270d6 100644
--- a/src/com/android/settings/applications/appinfo/AppButtonsPreferenceController.java
+++ b/src/com/android/settings/applications/appinfo/AppButtonsPreferenceController.java
@@ -226,7 +226,8 @@
                 uninstallDaIntent.putExtra(DeviceAdminAdd.EXTRA_DEVICE_ADMIN_PACKAGE_NAME,
                         packageName);
                 mMetricsFeatureProvider.action(mActivity,
-                        SettingsEnums.ACTION_SETTINGS_UNINSTALL_DEVICE_ADMIN);
+                        SettingsEnums.ACTION_SETTINGS_UNINSTALL_DEVICE_ADMIN,
+                        getPackageNameForMetric());
                 mFragment.startActivityForResult(uninstallDaIntent, mRequestRemoveDeviceAdmin);
                 return;
             }
@@ -253,7 +254,8 @@
                             mActivity,
                             mAppEntry.info.enabled
                                     ? SettingsEnums.ACTION_SETTINGS_DISABLE_APP
-                                    : SettingsEnums.ACTION_SETTINGS_ENABLE_APP);
+                                    : SettingsEnums.ACTION_SETTINGS_ENABLE_APP,
+                                    getPackageNameForMetric());
                     AsyncTask.execute(new DisableChangerRunnable(mPm, mAppEntry.info.packageName,
                             PackageManager.COMPONENT_ENABLED_STATE_DEFAULT));
                 }
@@ -270,7 +272,9 @@
         @Override
         public void onClick(View v) {
              mMetricsFeatureProvider.action(
-                     mActivity, SettingsEnums.ACTION_APP_INFO_FORCE_STOP);
+                     mActivity,
+                     SettingsEnums.ACTION_APP_INFO_FORCE_STOP,
+                     getPackageNameForMetric());
             // force stop
             if (mPm.isPackageStateProtected(mAppEntry.info.packageName, mUserId)) {
                 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mActivity,
@@ -757,6 +761,14 @@
                 || AppUtils.isMainlineModule(mPm, mAppEntry.info.packageName));
     }
 
+    private String getPackageNameForMetric() {
+        final String packageName =
+                mAppEntry != null && mAppEntry.info != null
+                        ? mAppEntry.info.packageName
+                        : null;
+        return packageName != null ? packageName : "";
+    }
+
     /**
      * Changes the status of disable/enable for a package
      */
diff --git a/src/com/android/settings/fuelgauge/BatteryInfo.java b/src/com/android/settings/fuelgauge/BatteryInfo.java
index 92151c0..d641e7b 100644
--- a/src/com/android/settings/fuelgauge/BatteryInfo.java
+++ b/src/com/android/settings/fuelgauge/BatteryInfo.java
@@ -25,6 +25,7 @@
 import android.os.BatteryUsageStats;
 import android.os.SystemClock;
 import android.text.format.Formatter;
+import android.util.Log;
 import android.util.SparseIntArray;
 
 import androidx.annotation.NonNull;
@@ -42,6 +43,7 @@
 import com.android.settingslib.utils.StringUtil;
 
 public class BatteryInfo {
+    private static final String TAG = "BatteryInfo";
 
     public CharSequence chargeLabel;
     public CharSequence remainingLabel;
@@ -155,8 +157,15 @@
                 if (batteryUsageStats != null) {
                     stats = batteryUsageStats;
                 } else {
-                    stats = context.getSystemService(BatteryStatsManager.class)
-                            .getBatteryUsageStats();
+                    try {
+                        stats = context.getSystemService(BatteryStatsManager.class)
+                                .getBatteryUsageStats();
+                    } catch (RuntimeException e) {
+                        Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e);
+                        // Use default BatteryUsageStats.
+                        stats = new BatteryUsageStats.Builder(
+                                new String[0], /* includePowerModels */ false).build();
+                    }
                 }
                 return getBatteryInfo(context, stats, shortString);
             }
diff --git a/src/com/android/settings/fuelgauge/BatteryOptimizeUtils.java b/src/com/android/settings/fuelgauge/BatteryOptimizeUtils.java
index 4a56040..21d2023 100644
--- a/src/com/android/settings/fuelgauge/BatteryOptimizeUtils.java
+++ b/src/com/android/settings/fuelgauge/BatteryOptimizeUtils.java
@@ -74,6 +74,14 @@
     }
 
     public void setAppUsageState(AppUsageState state) {
+        try {
+            setAppUsageStateInternal(state);
+        } catch (Exception e) {
+            Log.e(TAG, "setAppUsageState() is failed for " + mPackageName, e);
+        }
+    }
+
+    private void setAppUsageStateInternal(AppUsageState state) {
         switch (state) {
             case RESTRICTED:
                 mBatteryUtils.setForceAppStandby(mUid, mPackageName, AppOpsManager.MODE_IGNORED);
diff --git a/src/com/android/settings/fuelgauge/BatteryUsageStatsLoader.java b/src/com/android/settings/fuelgauge/BatteryUsageStatsLoader.java
index d35ef82..2e3200d 100644
--- a/src/com/android/settings/fuelgauge/BatteryUsageStatsLoader.java
+++ b/src/com/android/settings/fuelgauge/BatteryUsageStatsLoader.java
@@ -20,6 +20,7 @@
 import android.os.BatteryStatsManager;
 import android.os.BatteryUsageStats;
 import android.os.BatteryUsageStatsQuery;
+import android.util.Log;
 
 import com.android.settingslib.utils.AsyncLoaderCompat;
 
@@ -27,6 +28,7 @@
  * Loader to get new {@link BatteryUsageStats} in the background
  */
 public class BatteryUsageStatsLoader extends AsyncLoaderCompat<BatteryUsageStats> {
+    private static final String TAG = "BatteryUsageStatsLoader";
     private final BatteryStatsManager mBatteryStatsManager;
     private final boolean mIncludeBatteryHistory;
 
@@ -42,7 +44,14 @@
         if (mIncludeBatteryHistory) {
             builder.includeBatteryHistory();
         }
-        return mBatteryStatsManager.getBatteryUsageStats(builder.build());
+        try {
+            return mBatteryStatsManager.getBatteryUsageStats(builder.build());
+        } catch (RuntimeException e) {
+            Log.e(TAG, "loadInBackground() for getBatteryUsageStats()", e);
+            // Use default BatteryUsageStats.
+            return new BatteryUsageStats.Builder(
+                    new String[0], /* includePowerModels */ false).build();
+        }
     }
 
     @Override
diff --git a/src/com/android/settings/fuelgauge/BatteryUtils.java b/src/com/android/settings/fuelgauge/BatteryUtils.java
index d521ed1..1c458e1 100644
--- a/src/com/android/settings/fuelgauge/BatteryUtils.java
+++ b/src/com/android/settings/fuelgauge/BatteryUtils.java
@@ -368,8 +368,17 @@
     public BatteryInfo getBatteryInfo(final String tag) {
         final BatteryStatsManager systemService = mContext.getSystemService(
                 BatteryStatsManager.class);
-        final BatteryUsageStats batteryUsageStats = systemService.getBatteryUsageStats(
-                new BatteryUsageStatsQuery.Builder().includeBatteryHistory().build());
+        BatteryUsageStats batteryUsageStats;
+        try {
+            batteryUsageStats = systemService.getBatteryUsageStats(
+                    new BatteryUsageStatsQuery.Builder().includeBatteryHistory().build());
+        } catch (RuntimeException e) {
+            Log.e(TAG, "getBatteryInfo() error for getBatteryUsageStats()", e);
+            // Use default BatteryUsageStats.
+            batteryUsageStats =
+                    new BatteryUsageStats.Builder(new String[0], /* includePowerModels */ false)
+                            .build();
+        }
 
         final long startTime = System.currentTimeMillis();