mirror of
https://github.com/leanote/leanote-android.git
synced 2025-10-14 14:10:56 +00:00
适配appWidget
This commit is contained in:
@@ -4,6 +4,8 @@ import android.appwidget.AppWidgetManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.text.Html;
|
||||
import android.text.Spanned;
|
||||
import android.widget.RemoteViews;
|
||||
import android.widget.RemoteViewsService;
|
||||
|
||||
@@ -18,91 +20,91 @@ import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class AppWidgetViewsFactory implements RemoteViewsService.RemoteViewsFactory {
|
||||
private Context ctxt=null;
|
||||
private Context context = null;
|
||||
private int appWidgetId;
|
||||
private List<Note> allNotes;
|
||||
|
||||
public AppWidgetViewsFactory(Context ctxt, Intent intent) {
|
||||
this.ctxt=ctxt;
|
||||
appWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
|
||||
AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||
public AppWidgetViewsFactory(Context context, Intent intent) {
|
||||
this.context = context;
|
||||
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
|
||||
AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||
|
||||
allNotes = NoteDataStore.getAllNotes(Account.getCurrent().getUserId());
|
||||
Collections.sort(allNotes, new Comparator<Note>() {
|
||||
@Override
|
||||
public int compare(Note o1, Note o2) {
|
||||
String temp1=o1.getUpdatedTimeVal()+"";
|
||||
String temp2=o2.getUpdatedTimeVal()+"";
|
||||
String temp1 = o1.getUpdatedTimeVal() + "";
|
||||
String temp2 = o2.getUpdatedTimeVal() + "";
|
||||
return temp2.compareTo(temp1);
|
||||
}
|
||||
});
|
||||
if(allNotes.size()>30){
|
||||
allNotes=allNotes.subList(0,30);
|
||||
if (allNotes.size() > 30) {
|
||||
allNotes = allNotes.subList(0, 30);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
// no-op
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
// no-op
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return(allNotes.size());
|
||||
return allNotes.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteViews getViewAt(int position) {
|
||||
RemoteViews row=new RemoteViews(ctxt.getPackageName(),
|
||||
R.layout.note_app_widget_row);
|
||||
row.setTextViewText(R.id.appwidget_row_title, allNotes.get(position).getTitle());
|
||||
String textTemp="";
|
||||
if(allNotes.get(position).getContent().length()>200){
|
||||
textTemp=allNotes.get(position).getContent().substring(0,200)+"...";
|
||||
}else {
|
||||
textTemp=allNotes.get(position).getContent();
|
||||
|
||||
RemoteViews row = new RemoteViews(context.getPackageName(),
|
||||
R.layout.note_app_widget_row);
|
||||
Note note = allNotes.get(position);
|
||||
row.setTextViewText(R.id.appwidget_row_title, note.getTitle());
|
||||
String content;
|
||||
if (note.isMarkDown()) {
|
||||
content = note.getNoteAbstract();
|
||||
} else {
|
||||
Spanned spannedContent = Html.fromHtml(note.getNoteAbstract());
|
||||
content = spannedContent.toString().replaceAll("\\n\\n+", "\n");
|
||||
}
|
||||
row.setTextViewText(R.id.appwidget_row_text, textTemp);
|
||||
if (content.length() >= 200) {
|
||||
content = content.substring(0, 200) + "...";
|
||||
}
|
||||
row.setTextViewText(R.id.appwidget_row_text, content);
|
||||
|
||||
Intent intent=new Intent();
|
||||
|
||||
Bundle extras=new Bundle();
|
||||
extras.putLong(NotePreviewActivity.EXT_NOTE_LOCAL_ID, allNotes.get(position).getId());
|
||||
Intent intent = new Intent();
|
||||
Bundle extras = new Bundle();
|
||||
extras.putLong(NotePreviewActivity.EXT_NOTE_LOCAL_ID, note.getId());
|
||||
intent.putExtras(extras);
|
||||
|
||||
row.setOnClickFillInIntent(R.id.widget_note, intent);
|
||||
return(row);
|
||||
return row;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteViews getLoadingView() {
|
||||
return(null);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getViewTypeCount() {
|
||||
return(1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return(position);
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasStableIds() {
|
||||
return(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataSetChanged() {
|
||||
// no-op
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
|
||||
}
|
||||
}
|
@@ -6,7 +6,6 @@ import android.appwidget.AppWidgetProvider;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import org.houxg.leamonax.R;
|
||||
@@ -18,56 +17,30 @@ public class NoteAppWidget extends AppWidgetProvider {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
super.onReceive(context, intent);
|
||||
Log.i("debug!!!!!!!!!!!!!!!!","接收到广播");
|
||||
// abortBroadcast();
|
||||
// for (int i=0;i<widgetIds.length;i++){
|
||||
// Intent svcIntent=new Intent(context, WidgetService.class);
|
||||
// RemoteViews widget=new RemoteViews(context.getPackageName(),
|
||||
// R.layout.note_app_widget);
|
||||
//
|
||||
// widget.setRemoteAdapter(widgetIds[i], R.id.words,
|
||||
// svcIntent);
|
||||
//
|
||||
// Intent clickIntent=new Intent(context, RedirActivity.class);
|
||||
// PendingIntent clickPI=PendingIntent
|
||||
// .getActivity(context, 0,
|
||||
// clickIntent,
|
||||
// PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
//
|
||||
// widget.setPendingIntentTemplate(R.id.words, clickPI);
|
||||
//
|
||||
// AppWidgetManager.getInstance(context).updateAppWidget(widgetIds[i], widget);
|
||||
//
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
widgetIds=appWidgetIds;
|
||||
for (int i=0; i<appWidgetIds.length; i++) {
|
||||
Intent svcIntent=new Intent(context, WidgetService.class);
|
||||
widgetIds = appWidgetIds;
|
||||
for (int i = 0; i < appWidgetIds.length; i++) {
|
||||
Intent svcIntent = new Intent(context, WidgetService.class);
|
||||
|
||||
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
|
||||
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
|
||||
|
||||
RemoteViews widget=new RemoteViews(context.getPackageName(),
|
||||
RemoteViews widget = new RemoteViews(context.getPackageName(),
|
||||
R.layout.note_app_widget);
|
||||
|
||||
widget.setRemoteAdapter(appWidgetIds[i], R.id.words,
|
||||
svcIntent);
|
||||
|
||||
Intent clickIntent=new Intent(context, RedirActivity.class);
|
||||
PendingIntent clickPI=PendingIntent
|
||||
Intent clickIntent = new Intent(context, RedirActivity.class);
|
||||
PendingIntent clickPI = PendingIntent
|
||||
.getActivity(context, 0,
|
||||
clickIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
widget.setPendingIntentTemplate(R.id.words, clickPI);
|
||||
|
||||
// Intent i=new Intent(ctxt,NotePreviewActivity.class);
|
||||
// i.putExtra(NotePreviewActivity.EXT_NOTE_LOCAL_ID, allNotes.get(position).getId());
|
||||
// row.setOnClickFillInIntent(R.id.widget_note, i);
|
||||
|
||||
appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
|
||||
}
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
||||
@@ -76,7 +49,6 @@ public class NoteAppWidget extends AppWidgetProvider {
|
||||
@Override
|
||||
public void onDeleted(Context context, int[] appWidgetIds) {
|
||||
// When the user deletes the widget, delete the preference associated with it.
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -1,108 +0,0 @@
|
||||
package org.houxg.leamonax.appwidget;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
|
||||
import org.houxg.leamonax.R;
|
||||
|
||||
|
||||
/**
|
||||
* The configuration screen for the {@link NoteAppWidget NoteAppWidget} AppWidget.
|
||||
*/
|
||||
public class NoteAppWidgetConfigureActivity extends Activity {
|
||||
|
||||
private static final String PREFS_NAME = "org.houxg.leamonax.appwidget.NoteAppWidget";
|
||||
private static final String PREF_PREFIX_KEY = "appwidget_";
|
||||
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
|
||||
EditText mAppWidgetText;
|
||||
View.OnClickListener mOnClickListener = new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
final Context context = NoteAppWidgetConfigureActivity.this;
|
||||
|
||||
// 单击按钮的时候,字符串存储在本地
|
||||
String widgetText = mAppWidgetText.getText().toString();
|
||||
// saveTitlePref(context, mAppWidgetId, widgetText);
|
||||
|
||||
// 更新app小部件是配置活动的职责
|
||||
// It is the responsibility of the configuration activity to update the app widget
|
||||
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
||||
NoteAppWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
|
||||
|
||||
// 确保我们将原始应用程序小部件Id传回
|
||||
// Make sure we pass back the original appWidgetId
|
||||
Intent resultValue = new Intent();
|
||||
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
|
||||
setResult(RESULT_OK, resultValue);
|
||||
finish();
|
||||
}
|
||||
};
|
||||
|
||||
public NoteAppWidgetConfigureActivity() {
|
||||
super();
|
||||
}
|
||||
|
||||
// 存储在本地的字符串对象
|
||||
// Write the prefix to the SharedPreferences object for this widget
|
||||
static void saveTitlePref(Context context, int appWidgetId, String text) {
|
||||
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
|
||||
prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
|
||||
prefs.apply();
|
||||
}
|
||||
|
||||
// 从共享首选项对象中读取这个小部件的前缀。
|
||||
// 如果没有保存的首选项,则从资源中获取默认值
|
||||
// Read the prefix from the SharedPreferences object for this widget.
|
||||
// If there is no preference saved, get the default from a resource
|
||||
static String loadTitlePref(Context context, int appWidgetId) {
|
||||
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
|
||||
String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
|
||||
if (titleValue != null) {
|
||||
return titleValue;
|
||||
} else {
|
||||
return context.getString(R.string.appwidget_text);
|
||||
}
|
||||
}
|
||||
|
||||
//在删除部件的时候删除sp里面的存储
|
||||
static void deleteTitlePref(Context context, int appWidgetId) {
|
||||
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
|
||||
prefs.remove(PREF_PREFIX_KEY + appWidgetId);
|
||||
prefs.apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
// Set the result to CANCELED. This will cause the widget host to cancel
|
||||
// out of the widget placement if the user presses the back button.
|
||||
setResult(RESULT_CANCELED);
|
||||
|
||||
setContentView(R.layout.note_app_widget_configure);
|
||||
mAppWidgetText = (EditText) findViewById(R.id.appwidget_text);
|
||||
findViewById(R.id.add_button).setOnClickListener(mOnClickListener);
|
||||
|
||||
// Find the widget id from the intent.
|
||||
Intent intent = getIntent();
|
||||
Bundle extras = intent.getExtras();
|
||||
if (extras != null) {
|
||||
mAppWidgetId = extras.getInt(
|
||||
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||
}
|
||||
|
||||
// If this activity was started with an intent without an app widget ID, finish with an error.
|
||||
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
mAppWidgetText.setText(loadTitlePref(NoteAppWidgetConfigureActivity.this, mAppWidgetId));
|
||||
}
|
||||
}
|
||||
|
@@ -3,25 +3,23 @@ package org.houxg.leamonax.appwidget;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.houxg.leamonax.ui.NotePreviewActivity;
|
||||
import org.houxg.leamonax.utils.ToastUtils;
|
||||
|
||||
public class RedirActivity extends Activity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Long word = getIntent().getLongExtra(NotePreviewActivity.EXT_NOTE_LOCAL_ID, -1);
|
||||
Intent intent = new Intent(RedirActivity.this, NotePreviewActivity.class);
|
||||
Bundle bundle = new Bundle();
|
||||
|
||||
Long word=getIntent().getLongExtra(NotePreviewActivity.EXT_NOTE_LOCAL_ID,-1);
|
||||
|
||||
Intent intent=new Intent(RedirActivity.this,NotePreviewActivity.class);
|
||||
Bundle bundle=new Bundle();
|
||||
|
||||
if(word==-1){
|
||||
Toast.makeText(this, "笔记编号错误", Toast.LENGTH_LONG).show();
|
||||
}else {
|
||||
bundle.putLong(NotePreviewActivity.EXT_NOTE_LOCAL_ID,word);
|
||||
if (word == -1) {
|
||||
ToastUtils.show(this, "笔记编号错误");
|
||||
} else {
|
||||
bundle.putLong(NotePreviewActivity.EXT_NOTE_LOCAL_ID, word);
|
||||
intent.putExtras(bundle);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
@@ -6,7 +6,6 @@ import android.widget.RemoteViewsService;
|
||||
public class WidgetService extends RemoteViewsService {
|
||||
@Override
|
||||
public RemoteViewsFactory onGetViewFactory(Intent intent) {
|
||||
return(new AppWidgetViewsFactory(this.getApplicationContext(),
|
||||
intent));
|
||||
return new AppWidgetViewsFactory(this.getApplicationContext(), intent);
|
||||
}
|
||||
}
|
@@ -66,8 +66,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
|
||||
}
|
||||
|
||||
public static NoteFragment newInstance() {
|
||||
NoteFragment fragment = new NoteFragment();
|
||||
return fragment;
|
||||
return new NoteFragment();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -5,7 +5,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/black"
|
||||
tools:context="org.houxg.leamonax.ui.AboutActivity">
|
||||
tools:context="org.houxg.leamonax.ui.PictureViewerActivity">
|
||||
|
||||
<com.github.piasy.biv.view.BigImageView
|
||||
android:id="@+id/big_image"
|
||||
|
@@ -1,5 +1,4 @@
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/relativeLayout"
|
||||
android:layout_width="match_parent"
|
||||
@@ -12,12 +11,8 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="48dp"
|
||||
android:background="@color/colorPrimary"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
android:paddingStart="5dp"
|
||||
android:paddingEnd="10dp"
|
||||
>
|
||||
android:paddingEnd="10dp">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/widget_top_icon"
|
||||
@@ -50,13 +45,12 @@
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<ListView
|
||||
android:id="@+id/words"
|
||||
android:divider="@null"
|
||||
android:layout_marginTop="2dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_below="@+id/title_layout_widget"
|
||||
android:background="@color/transparent"/>
|
||||
|
||||
|
@@ -1,27 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:text="@string/configure"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/appwidget_text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="text"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/add_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:text="@string/add_widget"/>
|
||||
|
||||
</LinearLayout>
|
@@ -1,9 +1,10 @@
|
||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:minWidth="146dip"
|
||||
android:minHeight="146dip"
|
||||
android:minResizeWidth="180dp"
|
||||
android:minWidth="250dp"
|
||||
android:minResizeHeight="250dp"
|
||||
android:minHeight="320dp"
|
||||
android:updatePeriodMillis="43200000"
|
||||
android:initialLayout="@layout/note_app_widget"
|
||||
android:autoAdvanceViewId="@+id/words"
|
||||
android:previewImage="@drawable/ic_launcher"
|
||||
android:resizeMode="vertical"
|
||||
/>
|
Reference in New Issue
Block a user