Merge "Add NPE check when showing error dialog."

This commit is contained in:
TreeHugger Robot
2018-06-21 15:15:26 +00:00
committed by Android (Google) Code Review

View File

@@ -22,6 +22,7 @@ import android.bluetooth.BluetoothProfile;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.provider.Settings; import android.provider.Settings;
import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -40,6 +41,9 @@ import androidx.annotation.VisibleForTesting;
* for creating dialogs. * for creating dialogs.
*/ */
public final class Utils { public final class Utils {
private static final String TAG = "BluetoothUtils";
static final boolean V = BluetoothUtils.V; // verbose logging static final boolean V = BluetoothUtils.V; // verbose logging
static final boolean D = BluetoothUtils.D; // regular logging static final boolean D = BluetoothUtils.D; // regular logging
@@ -107,11 +111,15 @@ public final class Utils {
String message = context.getString(messageResId, name); String message = context.getString(messageResId, name);
Context activity = manager.getForegroundActivity(); Context activity = manager.getForegroundActivity();
if (manager.isForegroundActivity()) { if (manager.isForegroundActivity()) {
try {
new AlertDialog.Builder(activity) new AlertDialog.Builder(activity)
.setTitle(R.string.bluetooth_error_title) .setTitle(R.string.bluetooth_error_title)
.setMessage(message) .setMessage(message)
.setPositiveButton(android.R.string.ok, null) .setPositiveButton(android.R.string.ok, null)
.show(); .show();
} catch (Exception e) {
Log.e(TAG, "Cannot show error dialog.", e);
}
} else { } else {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
} }