add: dynamic shortcuts for android N
This commit is contained in:
@@ -70,8 +70,8 @@ public class Shortcut {
|
||||
if (mIconRes != null) {
|
||||
throw new IllegalStateException("Cannot set both iconRes and icon");
|
||||
}
|
||||
if (icon.getByteCount() > 1024 * 100) {
|
||||
mIcon = BitmapTool.scaleBitmap(icon, 100, 100);
|
||||
if (icon.getByteCount() > 1024 * 500) {
|
||||
mIcon = BitmapTool.scaleBitmap(icon, 200, 200);
|
||||
}else {
|
||||
mIcon = icon;
|
||||
}
|
||||
|
||||
66
app/src/main/java/com/stardust/scriptdroid/external/shortcut/ShortcutManager.java
vendored
Normal file
66
app/src/main/java/com/stardust/scriptdroid/external/shortcut/ShortcutManager.java
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
package com.stardust.scriptdroid.external.shortcut;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ShortcutInfo;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.RequiresApi;
|
||||
|
||||
import com.stardust.scriptdroid.external.CommonUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/10/25.
|
||||
*/
|
||||
|
||||
public class ShortcutManager {
|
||||
|
||||
private static ShortcutManager sInstance;
|
||||
private Context mContext;
|
||||
private android.content.pm.ShortcutManager mShortcutManager;
|
||||
|
||||
|
||||
public ShortcutManager(Context context) {
|
||||
mContext = context;
|
||||
mShortcutManager = (android.content.pm.ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
|
||||
}
|
||||
|
||||
public static ShortcutManager getInstance(Context context) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new ShortcutManager(context);
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
|
||||
public void addDynamicShortcut(CharSequence label, String id, Icon icon, Intent intent) {
|
||||
ShortcutInfo shortcut = new ShortcutInfo.Builder(mContext, id)
|
||||
.setIntent(intent)
|
||||
.setShortLabel(label)
|
||||
.setLongLabel(label)
|
||||
.setIcon(icon)
|
||||
.build();
|
||||
try {
|
||||
addDynamicShortcutUnchecked(shortcut);
|
||||
} catch (IllegalArgumentException shortcutsExceeded) {
|
||||
removeTheFirstShortcut();
|
||||
addDynamicShortcutUnchecked(shortcut);
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
|
||||
private void removeTheFirstShortcut() {
|
||||
List<ShortcutInfo> dynamicShortcuts = mShortcutManager.getDynamicShortcuts();
|
||||
mShortcutManager.removeDynamicShortcuts(Collections.singletonList(dynamicShortcuts.get(0).getId()));
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
|
||||
private void addDynamicShortcutUnchecked(ShortcutInfo shortcut) {
|
||||
mShortcutManager.addDynamicShortcuts(Collections.singletonList(shortcut));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,25 +4,27 @@ import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.PersistableBundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.external.CommonUtils;
|
||||
import com.stardust.scriptdroid.external.shortcut.Shortcut;
|
||||
import com.stardust.scriptdroid.external.shortcut.ShortcutActivity;
|
||||
import com.stardust.scriptdroid.external.shortcut.ShortcutManager;
|
||||
import com.stardust.scriptdroid.model.script.ScriptFile;
|
||||
import com.stardust.scriptdroid.tool.BitmapTool;
|
||||
import com.stardust.theme.dialog.ThemeColorMaterialDialogBuilder;
|
||||
import com.stardust.theme.internal.DrawableTool;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
@@ -45,9 +47,12 @@ public class ShortcutCreateActivity extends AppCompatActivity {
|
||||
@BindView(R.id.name)
|
||||
TextView mName;
|
||||
|
||||
@BindView(R.id.select_icon)
|
||||
@BindView(R.id.icon)
|
||||
ImageView mIcon;
|
||||
|
||||
@BindView(R.id.use_android_n_shortcut)
|
||||
CheckBox mUseAndroidNShortcut;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -58,6 +63,8 @@ public class ShortcutCreateActivity extends AppCompatActivity {
|
||||
private void showDialog() {
|
||||
View view = View.inflate(this, R.layout.shortcut_create_dialog, null);
|
||||
ButterKnife.bind(this, view);
|
||||
mUseAndroidNShortcut.setVisibility(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ?
|
||||
View.VISIBLE : View.GONE);
|
||||
mName.setText(mScriptFile.getSimplifiedName());
|
||||
new ThemeColorMaterialDialogBuilder(this)
|
||||
.customView(view, false)
|
||||
@@ -72,7 +79,7 @@ public class ShortcutCreateActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
|
||||
@OnClick(R.id.select_icon)
|
||||
@OnClick(R.id.icon)
|
||||
void selectIcon() {
|
||||
ShortcutIconSelectActivity_.intent(this)
|
||||
.startForResult(21209);
|
||||
@@ -80,6 +87,10 @@ public class ShortcutCreateActivity extends AppCompatActivity {
|
||||
|
||||
|
||||
private void createShortcut() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && mUseAndroidNShortcut.isChecked()) {
|
||||
createShortcutForAndroidN();
|
||||
return;
|
||||
}
|
||||
Shortcut shortcut = new Shortcut(this);
|
||||
if (mIsDefaultIcon) {
|
||||
shortcut.iconRes(R.drawable.ic_node_js_black);
|
||||
@@ -93,6 +104,23 @@ public class ShortcutCreateActivity extends AppCompatActivity {
|
||||
.send();
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
|
||||
private void createShortcutForAndroidN() {
|
||||
Icon icon;
|
||||
if (mIsDefaultIcon) {
|
||||
icon = Icon.createWithResource(this, R.drawable.ic_node_js_black);
|
||||
} else {
|
||||
Bitmap bitmap = BitmapTool.drawableToBitmap(mIcon.getDrawable());
|
||||
icon = Icon.createWithBitmap(bitmap);
|
||||
}
|
||||
PersistableBundle extras = new PersistableBundle(1);
|
||||
extras.putString(CommonUtils.EXTRA_KEY_PATH, mScriptFile.getPath());
|
||||
ShortcutManager.getInstance(this).addDynamicShortcut(mName.getText(), mScriptFile.getPath(), icon,
|
||||
new Intent(this, ShortcutActivity.class)
|
||||
.putExtra(CommonUtils.EXTRA_KEY_PATH, mScriptFile.getPath())
|
||||
.setAction(Intent.ACTION_MAIN));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (resultCode != RESULT_OK) {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.stardust.scriptdroid.ui.shortcut;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ShortcutManager;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.v7.widget.GridLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
|
||||
@@ -2,30 +2,44 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:padding="16dp">
|
||||
android:orientation="vertical">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/select_icon"
|
||||
android:layout_width="64dp"
|
||||
android:layout_height="64dp"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:background="@drawable/shortcut_create_dialog_icon_bg"
|
||||
android:padding="12dp"
|
||||
android:src="@drawable/ic_add_white_48dp"/>
|
||||
|
||||
<android.support.design.widget.TextInputLayout
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_vertical">
|
||||
android:orientation="horizontal"
|
||||
android:padding="16dp">
|
||||
|
||||
<android.support.design.widget.TextInputEditText
|
||||
android:id="@+id/name"
|
||||
<ImageView
|
||||
android:id="@+id/icon"
|
||||
android:layout_width="64dp"
|
||||
android:layout_height="64dp"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:background="@drawable/shortcut_create_dialog_icon_bg"
|
||||
android:padding="12dp"
|
||||
android:src="@drawable/ic_add_white_48dp"/>
|
||||
|
||||
<android.support.design.widget.TextInputLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/text_name"/>
|
||||
android:layout_gravity="center_vertical">
|
||||
|
||||
</android.support.design.widget.TextInputLayout>
|
||||
<android.support.design.widget.TextInputEditText
|
||||
android:id="@+id/name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/text_name"/>
|
||||
|
||||
</android.support.design.widget.TextInputLayout>
|
||||
</LinearLayout>
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/use_android_n_shortcut"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="20dp"
|
||||
android:layout_marginTop="4dp"
|
||||
android:text="@string/text_use_android_n_shortcut"/>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -265,6 +265,7 @@
|
||||
<string name="text_search">搜索</string>
|
||||
<string name="text_floating_edit">悬浮编辑</string>
|
||||
<string name="text_select_icon">选择图标</string>
|
||||
<string name="text_use_android_n_shortcut">添加到图标快捷方式</string>
|
||||
|
||||
|
||||
<string-array name="ad_showing_mode_keys">
|
||||
|
||||
Reference in New Issue
Block a user