Merge "Add icon to "Open [safety app] button" into sc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
9bf1412af7
@@ -23,11 +23,13 @@
|
||||
<Button
|
||||
android:id="@+id/button"
|
||||
style="@style/ActionPrimaryButton"
|
||||
android:theme="@style/RoundedCornerThemeOverlay"
|
||||
android:paddingHorizontal="16dp"
|
||||
android:drawablePadding="8dp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginVertical="8dp"
|
||||
android:layout_marginHorizontal="16dp"
|
||||
android:theme="@style/RoundedCornerThemeOverlay"
|
||||
settings:allowDividerBelow="true"/>
|
||||
|
||||
</FrameLayout>
|
||||
|
@@ -25,9 +25,15 @@ import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.util.TypedValue;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
@@ -35,6 +41,7 @@ import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settingslib.widget.LayoutPreference;
|
||||
|
||||
@@ -77,7 +84,17 @@ public class MoreSettingsPreferenceController extends BasePreferenceController i
|
||||
super.displayPreference(screen);
|
||||
final LayoutPreference pref = screen.findPreference(getPreferenceKey());
|
||||
final Button button = pref.findViewById(R.id.button);
|
||||
final Drawable icon = getIcon();
|
||||
button.setText(getButtonText());
|
||||
if (icon != null) {
|
||||
button.setCompoundDrawablesWithIntrinsicBounds(
|
||||
/* left= */ icon,
|
||||
/* top= */null,
|
||||
/* right= */ null,
|
||||
/* bottom= */ null);
|
||||
button.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
button.setOnClickListener(this);
|
||||
}
|
||||
|
||||
@@ -100,6 +117,21 @@ public class MoreSettingsPreferenceController extends BasePreferenceController i
|
||||
mContext.startActivity(intent, bundle);
|
||||
}
|
||||
|
||||
private Drawable getIcon() {
|
||||
final String packageName = mContext.getResources().getString(
|
||||
R.string.config_emergency_package_name);
|
||||
try {
|
||||
final PackageManager pm = mContext.getPackageManager();
|
||||
final ApplicationInfo appInfo = pm.getApplicationInfo(
|
||||
packageName, MATCH_DISABLED_COMPONENTS
|
||||
| MATCH_DISABLED_UNTIL_USED_COMPONENTS);
|
||||
return getScaledDrawable(mContext, Utils.getBadgedIcon(mContext, appInfo), 24, 24);
|
||||
} catch (Exception e) {
|
||||
Log.d(TAG, "Failed to get open app button icon", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private CharSequence getButtonText() {
|
||||
final String packageName = mContext.getResources().getString(
|
||||
R.string.config_emergency_package_name);
|
||||
@@ -114,4 +146,29 @@ public class MoreSettingsPreferenceController extends BasePreferenceController i
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private static Drawable getScaledDrawable(Context context, Drawable icon, int width,
|
||||
int height) {
|
||||
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
|
||||
int widthInDp =
|
||||
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width, displayMetrics);
|
||||
int heightInDp =
|
||||
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height,
|
||||
displayMetrics);
|
||||
|
||||
return new BitmapDrawable(context.getResources(),
|
||||
convertToBitmap(icon, widthInDp, heightInDp));
|
||||
}
|
||||
|
||||
private static Bitmap convertToBitmap(Drawable icon, int width, int height) {
|
||||
if (icon == null) {
|
||||
return null;
|
||||
}
|
||||
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
icon.setBounds(0, 0, width, height);
|
||||
icon.draw(canvas);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user