add widget support
This commit is contained in:
@@ -80,6 +80,11 @@
|
|||||||
<activity android:name=".ui.help.LocalWebViewActivity"/>
|
<activity android:name=".ui.help.LocalWebViewActivity"/>
|
||||||
<activity android:name=".external.tasker.TaskerScriptEditActivity_"/>
|
<activity android:name=".external.tasker.TaskerScriptEditActivity_"/>
|
||||||
<activity android:name=".ui.edit.ViewSampleActivity"/>
|
<activity android:name=".ui.edit.ViewSampleActivity"/>
|
||||||
|
<activity android:name=".external.widget.ScriptWidgetSettingsActivity_">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
<activity
|
<activity
|
||||||
android:name="com.stardust.autojs.runtime.api.image.ScreenCaptureRequestActivity"
|
android:name="com.stardust.autojs.runtime.api.image.ScreenCaptureRequestActivity"
|
||||||
android:taskAffinity="com.stardust.autojs.runtime.api.image.ScreenCaptureRequestActivity"
|
android:taskAffinity="com.stardust.autojs.runtime.api.image.ScreenCaptureRequestActivity"
|
||||||
@@ -241,6 +246,15 @@
|
|||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
|
<receiver android:name=".external.widget.ScriptWidget">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
|
||||||
|
</intent-filter>
|
||||||
|
<meta-data
|
||||||
|
android:name="android.appwidget.provider"
|
||||||
|
android:resource="@xml/script_widget_config"/>
|
||||||
|
</receiver>
|
||||||
|
|
||||||
<!-- 声明广告SDK所需要的组件 -->
|
<!-- 声明广告SDK所需要的组件 -->
|
||||||
<service
|
<service
|
||||||
android:name="com.qq.e.comm.DownloadService"
|
android:name="com.qq.e.comm.DownloadService"
|
||||||
|
|||||||
58
app/src/main/java/com/stardust/scriptdroid/external/widget/ScriptWidget.java
vendored
Normal file
58
app/src/main/java/com/stardust/scriptdroid/external/widget/ScriptWidget.java
vendored
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package com.stardust.scriptdroid.external.widget;
|
||||||
|
|
||||||
|
import android.app.PendingIntent;
|
||||||
|
import android.appwidget.AppWidgetManager;
|
||||||
|
import android.appwidget.AppWidgetProvider;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.RemoteViews;
|
||||||
|
|
||||||
|
import com.stardust.pio.PFile;
|
||||||
|
import com.stardust.scriptdroid.R;
|
||||||
|
import com.stardust.scriptdroid.external.CommonUtils;
|
||||||
|
import com.stardust.scriptdroid.external.open.RunIntentActivity;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Stardust on 2017/7/11.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ScriptWidget extends AppWidgetProvider {
|
||||||
|
|
||||||
|
private static final String LOG_TAG = "ScriptWidget";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||||
|
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
||||||
|
Set<Integer> appWidgetIdSet = new HashSet<>();
|
||||||
|
for (int appWidgetId : appWidgetIds) {
|
||||||
|
updateWidget(context, appWidgetId, ScriptWidgets.getPathForAppWidgetId(appWidgetId));
|
||||||
|
appWidgetIdSet.add(appWidgetId);
|
||||||
|
}
|
||||||
|
if (appWidgetIdSet.size() > 1)
|
||||||
|
ScriptWidgets.removeAllNotIn(appWidgetIdSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
static boolean updateWidget(Context context, int widgetId, String path) {
|
||||||
|
if (TextUtils.isEmpty(path) || widgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String name = PFile.getNameWithoutExtension(path);
|
||||||
|
int requestCode = ScriptWidgets.getRequestCodeForAppWidgetId(widgetId);
|
||||||
|
Log.d(LOG_TAG, "updateWidget: id = " + widgetId + ", requestCode = " + requestCode + ", path = " + path);
|
||||||
|
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
||||||
|
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_script_shortcut);
|
||||||
|
views.setOnClickPendingIntent(R.id.widget, PendingIntent.getActivity(context, requestCode,
|
||||||
|
new Intent(context, RunIntentActivity.class)
|
||||||
|
.putExtra(CommonUtils.EXTRA_KEY_PATH, path)
|
||||||
|
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT));
|
||||||
|
views.setTextViewText(R.id.name, name);
|
||||||
|
appWidgetManager.updateAppWidget(widgetId, views);
|
||||||
|
ScriptWidgets.setPathForAppWidgetId(widgetId, path);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
100
app/src/main/java/com/stardust/scriptdroid/external/widget/ScriptWidgetSettingsActivity.java
vendored
Normal file
100
app/src/main/java/com/stardust/scriptdroid/external/widget/ScriptWidgetSettingsActivity.java
vendored
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package com.stardust.scriptdroid.external.widget;
|
||||||
|
|
||||||
|
import android.app.PendingIntent;
|
||||||
|
import android.appwidget.AppWidgetManager;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
import android.widget.RemoteViews;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.stardust.scriptdroid.R;
|
||||||
|
import com.stardust.scriptdroid.external.CommonUtils;
|
||||||
|
import com.stardust.scriptdroid.external.open.RunIntentActivity;
|
||||||
|
import com.stardust.scriptdroid.external.tasker.TaskerScriptEditActivity;
|
||||||
|
import com.stardust.scriptdroid.script.ScriptFile;
|
||||||
|
import com.stardust.scriptdroid.script.StorageScriptProvider;
|
||||||
|
import com.stardust.scriptdroid.ui.BaseActivity;
|
||||||
|
import com.stardust.scriptdroid.ui.main.script_list.ScriptAndFolderListRecyclerView;
|
||||||
|
import com.stardust.scriptdroid.ui.main.script_list.ScriptListWithProgressBarView;
|
||||||
|
|
||||||
|
import org.androidannotations.annotations.AfterViews;
|
||||||
|
import org.androidannotations.annotations.Click;
|
||||||
|
import org.androidannotations.annotations.EActivity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Stardust on 2017/7/11.
|
||||||
|
*/
|
||||||
|
@EActivity(R.layout.activity_script_widget_settings)
|
||||||
|
public class ScriptWidgetSettingsActivity extends BaseActivity {
|
||||||
|
|
||||||
|
|
||||||
|
private String mSelectedScriptFilePath;
|
||||||
|
private StorageScriptProvider mStorageScriptProvider;
|
||||||
|
private int mAppWidgetId;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
mAppWidgetId = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterViews
|
||||||
|
void setUpViews() {
|
||||||
|
BaseActivity.setToolbarAsBack(this, R.id.toolbar, getString(R.string.text_please_choose_a_script));
|
||||||
|
initScriptListRecyclerView();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void initScriptListRecyclerView() {
|
||||||
|
mStorageScriptProvider = StorageScriptProvider.getExternalStorageProvider();
|
||||||
|
ScriptListWithProgressBarView scriptList = (ScriptListWithProgressBarView) findViewById(R.id.script_list);
|
||||||
|
scriptList.setScriptFileOperationEnabled(false);
|
||||||
|
scriptList.setStorageScriptProvider(mStorageScriptProvider);
|
||||||
|
scriptList.setCurrentDirectory(StorageScriptProvider.DEFAULT_DIRECTORY);
|
||||||
|
scriptList.setOnItemClickListener(new ScriptAndFolderListRecyclerView.OnScriptFileClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(ScriptFile file, int position) {
|
||||||
|
mSelectedScriptFilePath = file.getPath();
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
if (item.getItemId() == R.id.action_refresh) {
|
||||||
|
mStorageScriptProvider.refreshAll();
|
||||||
|
} else if (item.getItemId() == R.id.action_clear_file_selection) {
|
||||||
|
mSelectedScriptFilePath = null;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
|
getMenuInflater().inflate(R.menu.script_widget_settings_menu, menu);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void finish() {
|
||||||
|
if (ScriptWidget.updateWidget(this, mAppWidgetId, mSelectedScriptFilePath)) {
|
||||||
|
setResult(RESULT_OK, new Intent()
|
||||||
|
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
setResult(RESULT_CANCELED, new Intent()
|
||||||
|
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId));
|
||||||
|
}
|
||||||
|
super.finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
68
app/src/main/java/com/stardust/scriptdroid/external/widget/ScriptWidgets.java
vendored
Normal file
68
app/src/main/java/com/stardust/scriptdroid/external/widget/ScriptWidgets.java
vendored
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package com.stardust.scriptdroid.external.widget;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.stardust.scriptdroid.App;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Stardust on 2017/7/11.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ScriptWidgets {
|
||||||
|
|
||||||
|
|
||||||
|
private static final String LOG_TAG = "ScriptWidgets";
|
||||||
|
private static SharedPreferences widgets = App.getApp().getSharedPreferences("ScriptWidgets", Context.MODE_PRIVATE);
|
||||||
|
private static final Pattern ID_PATTERN = Pattern.compile("[a-zA-Z]+_([0-9])+");
|
||||||
|
|
||||||
|
static String getPathForAppWidgetId(int id) {
|
||||||
|
return widgets.getString("path_" + id, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setPathForAppWidgetId(int id, String path) {
|
||||||
|
widgets.edit().putString("path_" + id, path).apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getRequestCodeForAppWidgetId(int id) {
|
||||||
|
int requestCode = widgets.getInt("rc_" + id, -1);
|
||||||
|
if (requestCode == -1) {
|
||||||
|
requestCode = widgets.getInt("max_rc", 0) + 1;
|
||||||
|
widgets.edit()
|
||||||
|
.putInt("rc_" + id, requestCode)
|
||||||
|
.putInt("max_rc", requestCode)
|
||||||
|
.apply();
|
||||||
|
}
|
||||||
|
return requestCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void removeAllNotIn(Set<Integer> appWidgetIdSet) {
|
||||||
|
List<String> keysToRemove = new LinkedList<>();
|
||||||
|
for (Map.Entry<String, ?> entry : widgets.getAll().entrySet()) {
|
||||||
|
Matcher matcher = ID_PATTERN.matcher(entry.getKey());
|
||||||
|
if (matcher.find()) {
|
||||||
|
int id = Integer.parseInt(matcher.group(1));
|
||||||
|
if (!appWidgetIdSet.contains(id)) {
|
||||||
|
keysToRemove.add(entry.getKey());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.e(LOG_TAG, "illegal key: " + entry.getKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SharedPreferences.Editor editor = widgets.edit();
|
||||||
|
for (String key : keysToRemove) {
|
||||||
|
editor.remove(key);
|
||||||
|
Log.v(LOG_TAG, "remove key: " + key);
|
||||||
|
}
|
||||||
|
editor.apply();
|
||||||
|
}
|
||||||
|
}
|
||||||
33
app/src/main/res/layout/activity_script_widget_settings.xml
Normal file
33
app/src/main/res/layout/activity_script_widget_settings.xml
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<android.support.design.widget.AppBarLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@android:color/transparent"
|
||||||
|
android:clipToPadding="false"
|
||||||
|
android:theme="@style/AppTheme.AppBarOverlay">
|
||||||
|
|
||||||
|
|
||||||
|
<com.stardust.theme.widget.ThemeColorToolbar
|
||||||
|
android:id="@+id/toolbar"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="?attr/actionBarSize"
|
||||||
|
app:popupTheme="@style/AppTheme.PopupOverlay"
|
||||||
|
app:title="@string/_app_name">
|
||||||
|
|
||||||
|
|
||||||
|
</com.stardust.theme.widget.ThemeColorToolbar>
|
||||||
|
</android.support.design.widget.AppBarLayout>
|
||||||
|
|
||||||
|
<com.stardust.scriptdroid.ui.main.script_list.ScriptListWithProgressBarView
|
||||||
|
android:id="@+id/script_list"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="#fafafa"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
25
app/src/main/res/layout/widget_script_shortcut.xml
Normal file
25
app/src/main/res/layout/widget_script_shortcut.xml
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/widget"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/icon"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:src="@drawable/ic_node_js_black"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/name"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="4dp"
|
||||||
|
android:gravity="center"
|
||||||
|
android:maxLines="1"
|
||||||
|
android:textColor="@android:color/white"
|
||||||
|
android:textSize="12sp"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
19
app/src/main/res/menu/script_widget_settings_menu.xml
Normal file
19
app/src/main/res/menu/script_widget_settings_menu.xml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_refresh"
|
||||||
|
android:icon="@drawable/ic_refresh_white_24dp"
|
||||||
|
android:orderInCategory="100"
|
||||||
|
android:title="@string/text_refresh"
|
||||||
|
app:showAsAction="ifRoom"/>
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_clear_file_selection"
|
||||||
|
android:orderInCategory="200"
|
||||||
|
android:title="@string/text_clear_file_selection"
|
||||||
|
app:showAsAction="never"/>
|
||||||
|
|
||||||
|
</menu>
|
||||||
8
app/src/main/res/xml/script_widget_config.xml
Normal file
8
app/src/main/res/xml/script_widget_config.xml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:configure="com.stardust.scriptdroid.external.widget.ScriptWidgetSettingsActivity_"
|
||||||
|
android:initialLayout="@layout/widget_script_shortcut"
|
||||||
|
android:minHeight="40dp"
|
||||||
|
android:minWidth="40dp"
|
||||||
|
android:previewImage="@drawable/ic_node_js_black"
|
||||||
|
android:resizeMode="none"/>
|
||||||
Reference in New Issue
Block a user