fix widget not working correctly after device reboot
This commit is contained in:
parent
f3a3b021dd
commit
697586c3a9
@ -219,6 +219,7 @@
|
|||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||||
<action android:name="org.fox.ttrss.WIDGET_FORCE_UPDATE" />
|
<action android:name="org.fox.ttrss.WIDGET_FORCE_UPDATE" />
|
||||||
|
<action android:name="org.fox.ttrss.WIDGET_UPDATE_RESULT" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
|
||||||
<meta-data
|
<meta-data
|
||||||
|
2
org.fox.ttrss/src/main/java/org/fox/ttrss/OnlineActivity.java
Normal file → Executable file
2
org.fox.ttrss/src/main/java/org/fox/ttrss/OnlineActivity.java
Normal file → Executable file
@ -314,7 +314,7 @@ public class OnlineActivity extends CommonActivity {
|
|||||||
public void onStop() {
|
public void onStop() {
|
||||||
super.onStop();
|
super.onStop();
|
||||||
|
|
||||||
Intent initialUpdateIntent = new Intent(SmallWidgetProvider.FORCE_UPDATE_ACTION);
|
Intent initialUpdateIntent = new Intent(SmallWidgetProvider.ACTION_REQUEST_UPDATE);
|
||||||
sendBroadcast(initialUpdateIntent);
|
sendBroadcast(initialUpdateIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
111
org.fox.ttrss/src/main/java/org/fox/ttrss/widget/SmallWidgetProvider.java
Normal file → Executable file
111
org.fox.ttrss/src/main/java/org/fox/ttrss/widget/SmallWidgetProvider.java
Normal file → Executable file
@ -15,35 +15,14 @@ import org.fox.ttrss.R;
|
|||||||
public class SmallWidgetProvider extends AppWidgetProvider {
|
public class SmallWidgetProvider extends AppWidgetProvider {
|
||||||
private final String TAG = this.getClass().getSimpleName();
|
private final String TAG = this.getClass().getSimpleName();
|
||||||
|
|
||||||
public static final String FORCE_UPDATE_ACTION = "org.fox.ttrss.WIDGET_FORCE_UPDATE";
|
public static final String ACTION_REQUEST_UPDATE = "org.fox.ttrss.WIDGET_FORCE_UPDATE";
|
||||||
|
public static final String ACTION_UPDATE_RESULT = "org.fox.ttrss.WIDGET_UPDATE_RESULT";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||||
Log.d(TAG, "onUpdate");
|
Log.d(TAG, "onUpdate");
|
||||||
|
|
||||||
final int N = appWidgetIds.length;
|
Intent intent = new Intent(context, OnlineActivity.class);
|
||||||
|
|
||||||
// Perform this loop procedure for each App Widget that belongs to this provider
|
|
||||||
for (int i=0; i<N; i++) {
|
|
||||||
int appWidgetId = appWidgetIds[i];
|
|
||||||
|
|
||||||
// Create an Intent to launch ExampleActivity
|
|
||||||
Intent intent = new Intent(context, OnlineActivity.class);
|
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
|
||||||
|
|
||||||
// Get the layout for the App Widget and attach an on-click listener
|
|
||||||
// to the button
|
|
||||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_small);
|
|
||||||
views.setOnClickPendingIntent(R.id.widget_main, pendingIntent);
|
|
||||||
|
|
||||||
// Tell the AppWidgetManager to perform an update on the current app widget
|
|
||||||
appWidgetManager.updateAppWidget(appWidgetId, views);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Intent updateIntent = new Intent(context, org.fox.ttrss.widget.WidgetUpdateService.class);
|
|
||||||
PendingIntent updatePendingIntent = PendingIntent.getService(context, 0, updateIntent, 0);
|
|
||||||
|
|
||||||
Intent intent = new Intent(context, org.fox.ttrss.OnlineActivity.class);
|
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
||||||
|
|
||||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_small);
|
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_small);
|
||||||
@ -51,54 +30,60 @@ public class SmallWidgetProvider extends AppWidgetProvider {
|
|||||||
|
|
||||||
appWidgetManager.updateAppWidget(appWidgetIds, views);
|
appWidgetManager.updateAppWidget(appWidgetIds, views);
|
||||||
|
|
||||||
try {
|
Intent serviceIntent = new Intent(context.getApplicationContext(), WidgetUpdateService.class);
|
||||||
updatePendingIntent.send();
|
context.startService(serviceIntent);
|
||||||
} catch (CanceledException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
} */
|
|
||||||
|
|
||||||
//RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_small);
|
|
||||||
|
|
||||||
/* final int N = appWidgetIds.length;
|
|
||||||
|
|
||||||
for (int i=0; i < N; i++) {
|
|
||||||
int appWidgetId = appWidgetIds[i];
|
|
||||||
|
|
||||||
Intent updateIntent = new Intent(context, org.fox.ttrss.widget.WidgetUpdateService.class);
|
|
||||||
PendingIntent updatePendingIntent = PendingIntent.getService(context, 0, updateIntent, 0);
|
|
||||||
|
|
||||||
Intent intent = new Intent(context, org.fox.ttrss.OnlineActivity.class);
|
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
|
||||||
|
|
||||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_small);
|
|
||||||
views.setOnClickPendingIntent(R.id.widget_main, pendingIntent);
|
|
||||||
|
|
||||||
appWidgetManager.updateAppWidget(appWidgetId, views);
|
|
||||||
|
|
||||||
try {
|
|
||||||
updatePendingIntent.send();
|
|
||||||
} catch (CanceledException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
} */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
super.onReceive(context, intent);
|
|
||||||
|
|
||||||
Log.d(TAG, "onReceive");
|
Log.d(TAG, "onReceive");
|
||||||
|
|
||||||
if (FORCE_UPDATE_ACTION.equals(intent.getAction())) {
|
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
||||||
|
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), SmallWidgetProvider.class.getName());
|
||||||
|
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
|
||||||
|
|
||||||
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
if (ACTION_REQUEST_UPDATE.equals(intent.getAction())) {
|
||||||
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), SmallWidgetProvider.class.getName());
|
Log.d(TAG, "onReceive: got update request");
|
||||||
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
|
|
||||||
|
|
||||||
onUpdate(context, appWidgetManager, appWidgetIds);
|
onUpdate(context, appWidgetManager, appWidgetIds);
|
||||||
}
|
|
||||||
|
} else if (ACTION_UPDATE_RESULT.equals(intent.getAction())) {
|
||||||
|
int unread = intent.getIntExtra("unread", -1);
|
||||||
|
int resultCode = intent.getIntExtra("resultCode", WidgetUpdateService.UPDATE_RESULT_ERROR_OTHER);
|
||||||
|
|
||||||
|
Log.d(TAG, "onReceive: got update result from service: " + unread + " " + resultCode);
|
||||||
|
|
||||||
|
updateWidgetsText(context, appWidgetManager, appWidgetIds, unread, resultCode);
|
||||||
|
} else {
|
||||||
|
super.onReceive(context, intent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateWidgetsText(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, int unread, int resultCode) {
|
||||||
|
|
||||||
|
Intent intent = new Intent(context, OnlineActivity.class);
|
||||||
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
||||||
|
|
||||||
|
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_small);
|
||||||
|
views.setOnClickPendingIntent(R.id.widget_main, pendingIntent);
|
||||||
|
|
||||||
|
String viewText;
|
||||||
|
|
||||||
|
switch (resultCode) {
|
||||||
|
case WidgetUpdateService.UPDATE_RESULT_OK:
|
||||||
|
viewText = String.valueOf(unread);
|
||||||
|
break;
|
||||||
|
case WidgetUpdateService.UPDATE_IN_PROGRESS:
|
||||||
|
viewText = "...";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
viewText = "?";
|
||||||
|
}
|
||||||
|
|
||||||
|
views.setTextViewText(R.id.widget_unread_counter, viewText);
|
||||||
|
|
||||||
|
appWidgetManager.updateAppWidget(appWidgetIds, views);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
163
org.fox.ttrss/src/main/java/org/fox/ttrss/widget/WidgetUpdateService.java
Normal file → Executable file
163
org.fox.ttrss/src/main/java/org/fox/ttrss/widget/WidgetUpdateService.java
Normal file → Executable file
@ -1,138 +1,131 @@
|
|||||||
package org.fox.ttrss.widget;
|
package org.fox.ttrss.widget;
|
||||||
|
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.appwidget.AppWidgetManager;
|
|
||||||
import android.content.ComponentName;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.RemoteViews;
|
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import org.fox.ttrss.ApiRequest;
|
import org.fox.ttrss.ApiRequest;
|
||||||
import org.fox.ttrss.R;
|
|
||||||
import org.fox.ttrss.util.SimpleLoginManager;
|
import org.fox.ttrss.util.SimpleLoginManager;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class WidgetUpdateService extends Service {
|
public class WidgetUpdateService extends Service {
|
||||||
private final String TAG = this.getClass().getSimpleName();
|
private final String TAG = this.getClass().getSimpleName();
|
||||||
|
private SharedPreferences m_prefs;
|
||||||
|
|
||||||
@Override
|
public static final int UPDATE_RESULT_OK = 0;
|
||||||
|
public static final int UPDATE_RESULT_ERROR_LOGIN = 1;
|
||||||
|
public static final int UPDATE_RESULT_ERROR_OTHER = 2;
|
||||||
|
public static final int UPDATE_RESULT_ERROR_NEED_CONF = 3;
|
||||||
|
public static final int UPDATE_IN_PROGRESS = 4;
|
||||||
|
|
||||||
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
Log.d(TAG, "onBind");
|
Log.d(TAG, "onBind");
|
||||||
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @Override
|
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
||||||
Log.d(TAG, "onStartCommand");
|
|
||||||
|
|
||||||
return super.onStartCommand(intent, flags, startId);
|
|
||||||
} */
|
|
||||||
|
|
||||||
public void update() {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart(Intent intent, int startId) {
|
public void onStart(Intent intent, int startId) {
|
||||||
final RemoteViews view = new RemoteViews(getPackageName(), R.layout.widget_small);
|
Log.d(TAG, "onStart");
|
||||||
|
|
||||||
final ComponentName thisWidget = new ComponentName(this, SmallWidgetProvider.class);
|
try {
|
||||||
final AppWidgetManager manager = AppWidgetManager.getInstance(this);
|
sendResultIntent(-1, UPDATE_IN_PROGRESS);
|
||||||
|
|
||||||
try {
|
m_prefs = PreferenceManager
|
||||||
view.setTextViewText(R.id.counter, String.valueOf("..."));
|
.getDefaultSharedPreferences(getApplicationContext());
|
||||||
|
|
||||||
manager.updateAppWidget(thisWidget, view);
|
if (m_prefs.getString("ttrss_url", "").trim().length() == 0) {
|
||||||
|
|
||||||
final SharedPreferences m_prefs = PreferenceManager
|
sendResultIntent(-1, UPDATE_RESULT_ERROR_NEED_CONF);
|
||||||
.getDefaultSharedPreferences(getApplicationContext());
|
|
||||||
|
|
||||||
if (m_prefs.getString("ttrss_url", "").trim().length() == 0) {
|
} else {
|
||||||
|
|
||||||
// Toast: need configure
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
final int feedId = m_prefs.getBoolean("widget_show_fresh", true) ? -3 : 0;
|
final int feedId = m_prefs.getBoolean("widget_show_fresh", true) ? -3 : 0;
|
||||||
|
|
||||||
SimpleLoginManager loginManager = new SimpleLoginManager() {
|
SimpleLoginManager loginManager = new SimpleLoginManager() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onLoginSuccess(int requestId, String sessionId, int apiLevel) {
|
protected void onLoginSuccess(int requestId, String sessionId, int apiLevel) {
|
||||||
|
|
||||||
ApiRequest aru = new ApiRequest(getApplicationContext()) {
|
ApiRequest aru = new ApiRequest(getApplicationContext()) {
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(JsonElement result) {
|
protected void onPostExecute(JsonElement result) {
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
try {
|
try {
|
||||||
JsonObject content = result.getAsJsonObject();
|
JsonObject content = result.getAsJsonObject();
|
||||||
|
|
||||||
if (content != null) {
|
if (content != null) {
|
||||||
int unread = content.get("unread").getAsInt();
|
int unread = content.get("unread").getAsInt();
|
||||||
|
sendResultIntent(unread, UPDATE_RESULT_OK);
|
||||||
|
|
||||||
view.setTextViewText(R.id.counter, String.valueOf(unread));
|
return;
|
||||||
manager.updateAppWidget(thisWidget, view);
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
sendResultIntent(-1, UPDATE_RESULT_ERROR_OTHER);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
};
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
view.setTextViewText(R.id.counter, "?");
|
final String fSessionId = sessionId;
|
||||||
manager.updateAppWidget(thisWidget, view);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
final String fSessionId = sessionId;
|
HashMap<String, String> umap = new HashMap<String, String>() {
|
||||||
|
{
|
||||||
|
put("op", "getUnread");
|
||||||
|
put("feed_id", String.valueOf(feedId));
|
||||||
|
put("sid", fSessionId);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
HashMap<String, String> umap = new HashMap<String, String>() {
|
aru.execute(umap);
|
||||||
{
|
}
|
||||||
put("op", "getUnread");
|
|
||||||
put("feed_id", String.valueOf(feedId));
|
|
||||||
put("sid", fSessionId);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
aru.execute(umap);
|
@Override
|
||||||
}
|
protected void onLoginFailed(int requestId, ApiRequest ar) {
|
||||||
|
sendResultIntent(-1, UPDATE_RESULT_ERROR_LOGIN);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onLoginFailed(int requestId, ApiRequest ar) {
|
protected void onLoggingIn(int requestId) {
|
||||||
|
|
||||||
view.setTextViewText(R.id.counter, "?");
|
|
||||||
manager.updateAppWidget(thisWidget, view);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onLoggingIn(int requestId) {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
String login = m_prefs.getString("login", "").trim();
|
String login = m_prefs.getString("login", "").trim();
|
||||||
String password = m_prefs.getString("password", "").trim();
|
String password = m_prefs.getString("password", "").trim();
|
||||||
|
|
||||||
loginManager.logIn(getApplicationContext(), 1, login, password);
|
loginManager.logIn(getApplicationContext(), 1, login, password);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
||||||
view.setTextViewText(R.id.counter, "?");
|
sendResultIntent(-1, UPDATE_RESULT_ERROR_OTHER);
|
||||||
manager.updateAppWidget(thisWidget, view);
|
}
|
||||||
|
|
||||||
}
|
stopSelf();
|
||||||
|
|
||||||
|
super.onStart(intent, startId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sendResultIntent(int unread, int resultCode) {
|
||||||
|
Intent intent = new Intent();
|
||||||
|
intent.setAction(SmallWidgetProvider.ACTION_UPDATE_RESULT);
|
||||||
|
intent.putExtra("resultCode", resultCode);
|
||||||
|
intent.putExtra("unread", unread);
|
||||||
|
|
||||||
|
sendBroadcast(intent);
|
||||||
|
|
||||||
|
if (resultCode != UPDATE_IN_PROGRESS) stopSelf();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
4
org.fox.ttrss/src/main/res/layout/widget_small.xml
Normal file → Executable file
4
org.fox.ttrss/src/main/res/layout/widget_small.xml
Normal file → Executable file
@ -19,13 +19,13 @@
|
|||||||
android:src="@drawable/ic_launcher" />
|
android:src="@drawable/ic_launcher" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/counter"
|
android:id="@+id/widget_unread_counter"
|
||||||
android:layout_weight="0"
|
android:layout_weight="0"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_gravity="bottom|center"
|
android:layout_gravity="bottom|center"
|
||||||
android:gravity="center_horizontal"
|
android:gravity="center_horizontal"
|
||||||
android:text="\?"
|
android:text=""
|
||||||
android:textAppearance="?android:attr/textAppearanceSmall"
|
android:textAppearance="?android:attr/textAppearanceSmall"
|
||||||
android:textColor="@android:color/primary_text_dark"
|
android:textColor="@android:color/primary_text_dark"
|
||||||
android:textSize="13sp" />
|
android:textSize="13sp" />
|
||||||
|
Loading…
Reference in New Issue
Block a user