完成小部件分页功能

This commit is contained in:
ZhanGSKen 2025-02-16 20:54:33 +08:00
parent 691f9bbd1c
commit 7b05d613e4
10 changed files with 213 additions and 132 deletions

View File

@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Sat Feb 15 13:33:01 GMT 2025
#Sun Feb 16 12:53:20 GMT 2025
stageCount=2
libraryProject=libappbase
baseVersion=1.5
publishVersion=1.5.1
buildCount=147
buildCount=168
baseBetaVersion=1.5.2

View File

@ -15,7 +15,7 @@ import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.LogView;
import cc.winboll.studio.libappbase.SOS;
import cc.winboll.studio.libappbase.SimpleOperateSignalCenterService;
import cc.winboll.studio.libappbase.widgets.TimeWidget;
import cc.winboll.studio.libappbase.widgets.APPSOSReportWidget;
import com.hjq.toast.ToastUtils;
public class MainActivity extends AppCompatActivity {

View File

@ -8,7 +8,7 @@ import android.content.Context;
import android.content.Intent;
import cc.winboll.studio.appbase.handlers.MainServiceHandler;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.widgets.TimeWidget;
import cc.winboll.studio.libappbase.widgets.APPSOSReportWidget;
import java.lang.ref.WeakReference;
public class MainServiceThread extends Thread {
@ -43,8 +43,8 @@ public class MainServiceThread extends Thread {
while (!isExist()) {
//ToastUtils.show("run()");
LogUtils.d(TAG, "run()");
Intent intentTimeWidget = new Intent(mContext, TimeWidget.class);
intentTimeWidget.setAction(TimeWidget.UPDATE_TIME_ACTION);
Intent intentTimeWidget = new Intent(mContext, APPSOSReportWidget.class);
intentTimeWidget.setAction(APPSOSReportWidget.ACTION_ADD_SOS_REPORT);
intentTimeWidget.putExtra("appName", "TestName");
mContext.sendBroadcast(intentTimeWidget);

View File

@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Sat Feb 15 13:33:01 GMT 2025
#Sun Feb 16 12:53:20 GMT 2025
stageCount=2
libraryProject=libappbase
baseVersion=1.5
publishVersion=1.5.1
buildCount=147
buildCount=168
baseBetaVersion=1.5.2

View File

@ -41,17 +41,22 @@
</receiver>
<receiver android:name=".widgets.TimeWidget"
<receiver android:name=".widgets.APPSOSReportWidget"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.example.android.UPDATE_TIME" />
<action android:name="cc.winboll.studio.libappbase.widgets.APPSOSReportWidget.ACTION_ADD_SOS_REPORT" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_provider_info" />
</receiver>
<receiver android:name=".widgets.WidgetButtonClickListener">
<intent-filter>
<action android:name="cc.winboll.studio.libappbase.widgets.WidgetButtonClickListener.ACTION_PRE" />
<action android:name="cc.winboll.studio.libappbase.widgets.WidgetButtonClickListener.ACTION_NEXT" />
</intent-filter>
</receiver>
</application>
</manifest>

View File

@ -7,7 +7,7 @@ import android.content.Intent;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.WinBoll;
import cc.winboll.studio.libappbase.AppUtils;
import cc.winboll.studio.libappbase.widgets.TimeWidget;
import cc.winboll.studio.libappbase.widgets.APPSOSReportWidget;
/**
* @Author ZhanGSKen@AliYun.Com
@ -44,10 +44,10 @@ public class WinBollReceiver extends BroadcastReceiver {
String appName = AppUtils.getAppNameByPackageName(context, sosPackage);
LogUtils.d(TAG, String.format("appName %s", appName));
Intent intentTimeWidget = new Intent(context, TimeWidget.class);
intentTimeWidget.setAction(TimeWidget.UPDATE_TIME_ACTION);
intentTimeWidget.putExtra("appName", appName);
context.sendBroadcast(intentTimeWidget);
Intent intentAPPSOSReportWidget = new Intent(context, APPSOSReportWidget.class);
intentAPPSOSReportWidget.setAction(APPSOSReportWidget.ACTION_ADD_SOS_REPORT);
intentAPPSOSReportWidget.putExtra("appName", appName);
context.sendBroadcast(intentAPPSOSReportWidget);
}
}

View File

@ -0,0 +1,144 @@
package cc.winboll.studio.libappbase.widgets;
/**
* @Author ZhanGSKen@AliYun.Com
* @Date 2025/02/15 14:41:25
* @Describe TimeWidget
*/
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.R;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class APPSOSReportWidget extends AppWidgetProvider {
public static final String TAG = "APPSOSReportWidget";
public static final String ACTION_ADD_SOS_REPORT = "cc.winboll.studio.libappbase.widgets.APPSOSReportWidget.ACTION_ADD_SOS_REPORT";
public static final String ACTION_RELOAD_SOS_REPORT = "cc.winboll.studio.libappbase.widgets.APPSOSReportWidget.ACTION_RELOAD_SOS_REPORT";
volatile static ArrayList<String> _Message;
final static int _MAX_PAGES = 10;
final static int _OnePageLinesCount = 5;
volatile static int _CurrentPageIndex = 0;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId, "");
}
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(ACTION_ADD_SOS_REPORT)) {
LogUtils.d(TAG, "ACTION_ADD_SOS_REPORT");
String sosAppName = intent.getStringExtra("appName");
LogUtils.d(TAG, String.format("sosAppName %s", sosAppName));
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, APPSOSReportWidget.class));
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId, sosAppName);
}
} else if (intent.getAction().equals(ACTION_RELOAD_SOS_REPORT)) {
LogUtils.d(TAG, "ACTION_RELOAD_SOS_REPORT");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, APPSOSReportWidget.class));
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId, "");
}
}
}
private void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, String sosAppName) {
LogUtils.d(TAG, "updateAppWidget(...)");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//设置按钮点击事件
Intent intentPre = new Intent(context, WidgetButtonClickListener.class);
intentPre.setAction(WidgetButtonClickListener.ACTION_PRE);
PendingIntent pendingIntentPre = PendingIntent.getBroadcast(context, 0, intentPre, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_button_pre, pendingIntentPre);
Intent intentNext = new Intent(context, WidgetButtonClickListener.class);
intentNext.setAction(WidgetButtonClickListener.ACTION_NEXT);
PendingIntent pendingIntentNext = PendingIntent.getBroadcast(context, 0, intentNext, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_button_next, pendingIntentNext);
// 加入新消息
if (sosAppName != null && !sosAppName.equals("")) {
if (_Message == null) {
_Message = new ArrayList<String>();
}
// 获取当前时间并设置到TextView
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String currentTime = sdf.format(new Date());
StringBuilder sbLine = new StringBuilder();
sbLine.append("[");
sbLine.append(currentTime);
sbLine.append("] Power to ");
sbLine.append(sosAppName);
_Message.add(0, sbLine.toString());
while (_Message.size() > _MAX_PAGES * _OnePageLinesCount) { // 控制显示在6行
_Message.remove(_Message.size() - 1);
}
}
views.setTextViewText(R.id.infoTextView, getPageInfo());
views.setTextViewText(R.id.sosReportTextView, getMessage());
appWidgetManager.updateAppWidget(appWidgetId, views);
}
public static String getMessage() {
ArrayList<String> msgTemp = new ArrayList<String>();
if (_Message != null) {
int start = _OnePageLinesCount * _CurrentPageIndex;
start = _Message.size() > start ? start : _Message.size() - 1;
for (int i = start, j = 0; i < _Message.size() && j < _OnePageLinesCount; i++, j++) {
msgTemp.add(_Message.get(i));
}
String message = String.join("\n", msgTemp);
return message;
}
return "";
}
public static void prePage(Context context) {
if (_Message != null) {
if (_CurrentPageIndex > 0) {
_CurrentPageIndex = _CurrentPageIndex - 1;
}
Intent intentAPPSOSReportWidget = new Intent(context, APPSOSReportWidget.class);
intentAPPSOSReportWidget.setAction(APPSOSReportWidget.ACTION_RELOAD_SOS_REPORT);
context.sendBroadcast(intentAPPSOSReportWidget);
}
}
public static void nextPage(Context context) {
if (_Message != null) {
if ((_CurrentPageIndex + 1) * _OnePageLinesCount < _Message.size()) {
_CurrentPageIndex = _CurrentPageIndex + 1;
}
Intent intentAPPSOSReportWidget = new Intent(context, APPSOSReportWidget.class);
intentAPPSOSReportWidget.setAction(APPSOSReportWidget.ACTION_RELOAD_SOS_REPORT);
context.sendBroadcast(intentAPPSOSReportWidget);
}
}
String getPageInfo() {
if (_Message == null) {
return "0/0";
}
int leftCount = _Message.size() % _OnePageLinesCount;
int currentPageCount = _Message.size() / _OnePageLinesCount + (leftCount == 0 ?0: 1);
return String.format("%d/%d", _CurrentPageIndex + 1, currentPageCount);
}
}

View File

@ -1,83 +0,0 @@
package cc.winboll.studio.libappbase.widgets;
/**
* @Author ZhanGSKen@AliYun.Com
* @Date 2025/02/15 14:41:25
* @Describe TimeWidget
*/
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.R;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class TimeWidget extends AppWidgetProvider {
public static final String TAG = "TimeWidget";
public static final String UPDATE_TIME_ACTION = "com.example.android.UPDATE_TIME";
volatile static ArrayList<String> _Message;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId, "");
}
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(UPDATE_TIME_ACTION)) {
String sosAppName = intent.getStringExtra("appName");
LogUtils.d(TAG, String.format("sosAppName %s", sosAppName));
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, TimeWidget.class));
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId, sosAppName);
}
}
}
private void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, String sosAppName) {
LogUtils.d(TAG, "updateAppWidget(...)");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//设置按钮点击事件
Intent intent = new Intent(context, WidgetButtonClickListener.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_button, pendingIntent);
if (_Message == null) {
_Message = new ArrayList<String>();
}
// 获取当前时间并设置到TextView
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String currentTime = sdf.format(new Date());
StringBuilder sbLine = new StringBuilder();
sbLine.append("[");
sbLine.append(currentTime);
sbLine.append("] Power to ");
sbLine.append(sosAppName);
_Message.add(0, sbLine.toString());
while (_Message.size() > 6) { // 控制显示在6行
_Message.remove(_Message.size() - 1);
}
if (_Message != null) {
String message = String.join("\n", _Message);
views.setTextViewText(R.id.timeTextView, message);
}
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

View File

@ -5,29 +5,32 @@ package cc.winboll.studio.libappbase.widgets;
* @Date 2025/02/15 17:20:46
* @Describe WidgetButtonClickListener
*/
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;
import cc.winboll.studio.libappbase.R;
import cc.winboll.studio.libappbase.LogUtils;
public class WidgetButtonClickListener extends BroadcastReceiver {
public static final String TAG = "WidgetButtonClickListener";
public static final String ACTION_PRE = "cc.winboll.studio.libappbase.widgets.WidgetButtonClickListener.ACTION_PRE";
public static final String ACTION_NEXT = "cc.winboll.studio.libappbase.widgets.WidgetButtonClickListener.ACTION_NEXT";
@Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, TimeWidget.class));
for (int appWidgetId : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setTextViewText(R.id.timeTextView, "文本已更新");
appWidgetManager.updateAppWidget(appWidgetId, views);
String action = intent.getAction();
if (action == null) {
LogUtils.d(TAG, String.format("action %s", action));
return;
}
if (action.equals(ACTION_PRE)) {
LogUtils.d(TAG, "ACTION_PRE");
APPSOSReportWidget.prePage(context);
} else if (action.equals(ACTION_NEXT)) {
LogUtils.d(TAG, "ACTION_NEXT");
APPSOSReportWidget.nextPage(context);
} else {
LogUtils.d(TAG, String.format("action %s", action));
}
Toast.makeText(context, "按钮被点击", Toast.LENGTH_SHORT).show();
}
}

View File

@ -1,30 +1,42 @@
<?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:background="#FFFFFFFF">
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:background="#FFFFFFFF">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/infoTextView"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/widget_button"/>
android:text="⇦"
android:id="@+id/widget_button_pre"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="⇨"
android:id="@+id/widget_button_next"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/timeTextView"
android:layout_weight="1.0"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/sosReportTextView"
android:layout_weight="1.0"/>
</LinearLayout>