add: dynamic shortcuts for android N
This commit is contained in:
@@ -70,8 +70,8 @@ public class Shortcut {
|
|||||||
if (mIconRes != null) {
|
if (mIconRes != null) {
|
||||||
throw new IllegalStateException("Cannot set both iconRes and icon");
|
throw new IllegalStateException("Cannot set both iconRes and icon");
|
||||||
}
|
}
|
||||||
if (icon.getByteCount() > 1024 * 100) {
|
if (icon.getByteCount() > 1024 * 500) {
|
||||||
mIcon = BitmapTool.scaleBitmap(icon, 100, 100);
|
mIcon = BitmapTool.scaleBitmap(icon, 200, 200);
|
||||||
}else {
|
}else {
|
||||||
mIcon = icon;
|
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.content.pm.PackageManager;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.graphics.drawable.Icon;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.PersistableBundle;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
import android.support.annotation.RequiresApi;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.CheckBox;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.afollestad.materialdialogs.MaterialDialog;
|
|
||||||
import com.stardust.scriptdroid.R;
|
import com.stardust.scriptdroid.R;
|
||||||
import com.stardust.scriptdroid.external.CommonUtils;
|
import com.stardust.scriptdroid.external.CommonUtils;
|
||||||
import com.stardust.scriptdroid.external.shortcut.Shortcut;
|
import com.stardust.scriptdroid.external.shortcut.Shortcut;
|
||||||
import com.stardust.scriptdroid.external.shortcut.ShortcutActivity;
|
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.model.script.ScriptFile;
|
||||||
import com.stardust.scriptdroid.tool.BitmapTool;
|
import com.stardust.scriptdroid.tool.BitmapTool;
|
||||||
import com.stardust.theme.dialog.ThemeColorMaterialDialogBuilder;
|
import com.stardust.theme.dialog.ThemeColorMaterialDialogBuilder;
|
||||||
import com.stardust.theme.internal.DrawableTool;
|
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
|
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
@@ -45,9 +47,12 @@ public class ShortcutCreateActivity extends AppCompatActivity {
|
|||||||
@BindView(R.id.name)
|
@BindView(R.id.name)
|
||||||
TextView mName;
|
TextView mName;
|
||||||
|
|
||||||
@BindView(R.id.select_icon)
|
@BindView(R.id.icon)
|
||||||
ImageView mIcon;
|
ImageView mIcon;
|
||||||
|
|
||||||
|
@BindView(R.id.use_android_n_shortcut)
|
||||||
|
CheckBox mUseAndroidNShortcut;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@@ -58,6 +63,8 @@ public class ShortcutCreateActivity extends AppCompatActivity {
|
|||||||
private void showDialog() {
|
private void showDialog() {
|
||||||
View view = View.inflate(this, R.layout.shortcut_create_dialog, null);
|
View view = View.inflate(this, R.layout.shortcut_create_dialog, null);
|
||||||
ButterKnife.bind(this, view);
|
ButterKnife.bind(this, view);
|
||||||
|
mUseAndroidNShortcut.setVisibility(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ?
|
||||||
|
View.VISIBLE : View.GONE);
|
||||||
mName.setText(mScriptFile.getSimplifiedName());
|
mName.setText(mScriptFile.getSimplifiedName());
|
||||||
new ThemeColorMaterialDialogBuilder(this)
|
new ThemeColorMaterialDialogBuilder(this)
|
||||||
.customView(view, false)
|
.customView(view, false)
|
||||||
@@ -72,7 +79,7 @@ public class ShortcutCreateActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@OnClick(R.id.select_icon)
|
@OnClick(R.id.icon)
|
||||||
void selectIcon() {
|
void selectIcon() {
|
||||||
ShortcutIconSelectActivity_.intent(this)
|
ShortcutIconSelectActivity_.intent(this)
|
||||||
.startForResult(21209);
|
.startForResult(21209);
|
||||||
@@ -80,6 +87,10 @@ public class ShortcutCreateActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
|
|
||||||
private void createShortcut() {
|
private void createShortcut() {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && mUseAndroidNShortcut.isChecked()) {
|
||||||
|
createShortcutForAndroidN();
|
||||||
|
return;
|
||||||
|
}
|
||||||
Shortcut shortcut = new Shortcut(this);
|
Shortcut shortcut = new Shortcut(this);
|
||||||
if (mIsDefaultIcon) {
|
if (mIsDefaultIcon) {
|
||||||
shortcut.iconRes(R.drawable.ic_node_js_black);
|
shortcut.iconRes(R.drawable.ic_node_js_black);
|
||||||
@@ -93,6 +104,23 @@ public class ShortcutCreateActivity extends AppCompatActivity {
|
|||||||
.send();
|
.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
|
@Override
|
||||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
if (resultCode != RESULT_OK) {
|
if (resultCode != RESULT_OK) {
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package com.stardust.scriptdroid.ui.shortcut;
|
package com.stardust.scriptdroid.ui.shortcut;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.pm.ShortcutManager;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.support.v7.widget.GridLayoutManager;
|
import android.support.v7.widget.GridLayoutManager;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
|
|||||||
@@ -2,30 +2,44 @@
|
|||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="horizontal"
|
android:orientation="vertical">
|
||||||
android:padding="16dp">
|
|
||||||
|
|
||||||
<ImageView
|
<LinearLayout
|
||||||
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
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_gravity="center_vertical">
|
android:orientation="horizontal"
|
||||||
|
android:padding="16dp">
|
||||||
|
|
||||||
<android.support.design.widget.TextInputEditText
|
<ImageView
|
||||||
android:id="@+id/name"
|
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_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
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>
|
</LinearLayout>
|
||||||
@@ -265,6 +265,7 @@
|
|||||||
<string name="text_search">搜索</string>
|
<string name="text_search">搜索</string>
|
||||||
<string name="text_floating_edit">悬浮编辑</string>
|
<string name="text_floating_edit">悬浮编辑</string>
|
||||||
<string name="text_select_icon">选择图标</string>
|
<string name="text_select_icon">选择图标</string>
|
||||||
|
<string name="text_use_android_n_shortcut">添加到图标快捷方式</string>
|
||||||
|
|
||||||
|
|
||||||
<string-array name="ad_showing_mode_keys">
|
<string-array name="ad_showing_mode_keys">
|
||||||
|
|||||||
Reference in New Issue
Block a user