feat(MyTermuxActivity): 创建桌面快捷方式时使用按钮自定义图标
新增 loadButtonIcon() 加载模型 iconPath 对应图片, API 26+ 用 Icon.createWithBitmap,旧 API 用 EXTRA_SHORTCUT_ICON, 无图标时回退 ic_menu_manage。
This commit is contained in:
@@ -191,6 +191,14 @@ public class MyTermuxActivity extends AppCompatActivity {
|
|||||||
.show();
|
.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Bitmap loadButtonIcon(TermuxButtonModel model) {
|
||||||
|
String iconPath = model.getIconPath();
|
||||||
|
if (iconPath == null || iconPath.length() == 0) return null;
|
||||||
|
File iconFile = getIconFile(iconPath);
|
||||||
|
if (!iconFile.exists()) return null;
|
||||||
|
return BitmapFactory.decodeFile(iconFile.getAbsolutePath());
|
||||||
|
}
|
||||||
|
|
||||||
private void createDesktopShortcut(TermuxButtonModel model) {
|
private void createDesktopShortcut(TermuxButtonModel model) {
|
||||||
Intent shortcutIntent = new Intent(this, MyTermuxActivity.class);
|
Intent shortcutIntent = new Intent(this, MyTermuxActivity.class);
|
||||||
shortcutIntent.setAction(ACTION_EXECUTE_SHORTCUT);
|
shortcutIntent.setAction(ACTION_EXECUTE_SHORTCUT);
|
||||||
@@ -206,13 +214,20 @@ public class MyTermuxActivity extends AppCompatActivity {
|
|||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Icon icon;
|
||||||
|
Bitmap bmp = loadButtonIcon(model);
|
||||||
|
if (bmp != null) {
|
||||||
|
icon = Icon.createWithBitmap(bmp);
|
||||||
|
} else {
|
||||||
|
icon = Icon.createWithResource(this,
|
||||||
|
android.R.drawable.ic_menu_manage);
|
||||||
|
}
|
||||||
String shortcutId = "termux_" + model.getButtonName();
|
String shortcutId = "termux_" + model.getButtonName();
|
||||||
android.content.pm.ShortcutInfo info =
|
android.content.pm.ShortcutInfo info =
|
||||||
new android.content.pm.ShortcutInfo.Builder(this, shortcutId)
|
new android.content.pm.ShortcutInfo.Builder(this, shortcutId)
|
||||||
.setShortLabel(model.getButtonName())
|
.setShortLabel(model.getButtonName())
|
||||||
.setLongLabel(model.getButtonName())
|
.setLongLabel(model.getButtonName())
|
||||||
.setIcon(Icon.createWithResource(this,
|
.setIcon(icon)
|
||||||
android.R.drawable.ic_menu_manage))
|
|
||||||
.setIntent(shortcutIntent)
|
.setIntent(shortcutIntent)
|
||||||
.build();
|
.build();
|
||||||
manager.requestPinShortcut(info, null);
|
manager.requestPinShortcut(info, null);
|
||||||
@@ -228,9 +243,14 @@ public class MyTermuxActivity extends AppCompatActivity {
|
|||||||
installIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
|
installIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
|
||||||
installIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
|
installIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
|
||||||
model.getButtonName());
|
model.getButtonName());
|
||||||
installIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
|
Bitmap bmp = loadButtonIcon(model);
|
||||||
Intent.ShortcutIconResource.fromContext(this,
|
if (bmp != null) {
|
||||||
android.R.drawable.ic_menu_manage));
|
installIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bmp);
|
||||||
|
} else {
|
||||||
|
installIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
|
||||||
|
Intent.ShortcutIconResource.fromContext(this,
|
||||||
|
android.R.drawable.ic_menu_manage));
|
||||||
|
}
|
||||||
installIntent.putExtra("duplicate", false);
|
installIntent.putExtra("duplicate", false);
|
||||||
sendBroadcast(installIntent);
|
sendBroadcast(installIntent);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user