open the app at widget click

This commit is contained in:
tibbi 2016-05-31 17:26:01 +02:00
parent f30991c430
commit a28ed90bc6
2 changed files with 17 additions and 2 deletions

View File

@ -1,19 +1,21 @@
package com.simplemobiletools.notes; package com.simplemobiletools.notes;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider; import android.appwidget.AppWidgetProvider;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color; import android.graphics.Color;
import android.widget.RemoteViews; import android.widget.RemoteViews;
public class MyWidgetProvider extends AppWidgetProvider { public class MyWidgetProvider extends AppWidgetProvider {
private static SharedPreferences prefs; private static SharedPreferences prefs;
private RemoteViews remoteViews;
@Override @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
prefs = context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE); initVariables(context);
final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
final int defaultColor = context.getResources().getColor(R.color.dark_grey); final int defaultColor = context.getResources().getColor(R.color.dark_grey);
final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor); final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor);
final int newTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE); final int newTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
@ -26,6 +28,18 @@ public class MyWidgetProvider extends AppWidgetProvider {
super.onUpdate(context, appWidgetManager, appWidgetIds); super.onUpdate(context, appWidgetManager, appWidgetIds);
} }
private void initVariables(Context context) {
prefs = context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
setupIntent(R.id.notes_holder, context);
}
private void setupIntent(int id, Context context) {
final Intent intent = new Intent(context, MainActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(id, pendingIntent);
}
private void updateWidget(AppWidgetManager widgetManager, int widgetId, RemoteViews remoteViews) { private void updateWidget(AppWidgetManager widgetManager, int widgetId, RemoteViews remoteViews) {
final String text = prefs.getString(Constants.TEXT, ""); final String text = prefs.getString(Constants.TEXT, "");
remoteViews.setTextViewText(R.id.notes_view, text); remoteViews.setTextViewText(R.id.notes_view, text);

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout <RelativeLayout
android:id="@+id/notes_holder"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">