1 package com.google.android.leanbacklauncher.partnerwidget;
2 
3 import android.appwidget.AppWidgetManager;
4 import android.appwidget.AppWidgetProvider;
5 import android.content.ComponentName;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.net.ConnectivityManager;
9 import android.net.NetworkInfo;
10 import android.os.Bundle;
11 import android.widget.RemoteViews;
12 
13 public class ClockWidgetProvider extends AppWidgetProvider {
14 
15     private static final String SHARED_PREFS = "widget-prefs";
16     private static final int INET_CONDITION_THRESHOLD = 50;
17     private static final String INET_CONDITION_ACTION = "android.net.conn.INET_CONDITION_ACTION";
18     private static final String EXTRA_INET_CONDITION = "inetCondition";
19 
20     @Override
onReceive(Context context, Intent intent)21     public void onReceive(Context context, Intent intent) {
22         String intentAction = intent.getAction();
23 
24         if (INET_CONDITION_ACTION.equals(intentAction) ||
25                 ConnectivityManager.CONNECTIVITY_ACTION.equals(intentAction)) {
26 
27             if (INET_CONDITION_ACTION.equals(intentAction)) {
28                 // a broadcast with this intent action is only fired when we are actually connected
29                 // (i.e connectionStatus = 100).  So, clearing connectivity status when changing
30                 // networks is required
31                 int connectionStatus = intent.getIntExtra(EXTRA_INET_CONDITION, -551);
32                 writeConnectivity(context, connectionStatus > INET_CONDITION_THRESHOLD);
33             }
34 
35             update(context);
36         }
37 
38         super.onReceive(context, intent);
39     }
40 
41     @Override
onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions)42     public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
43             int appWidgetId, Bundle newOptions) {
44         update(context);
45     }
46 
onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)47     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
48         update(context);
49         super.onUpdate(context, appWidgetManager, appWidgetIds);
50     }
51 
update(Context context)52     private void update(Context context) {
53         AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
54         ComponentName thisWidget = new ComponentName(context, ClockWidgetProvider.class);
55         appWidgetManager.updateAppWidget(thisWidget, getRemoteViews(context));
56     }
57 
getRemoteViews(Context context)58     private RemoteViews getRemoteViews(Context context) {
59         RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.clock_widget);
60         remoteViews.setImageViewResource(R.id.connectivity_indicator, getConnectedResId(context));
61 
62         return remoteViews;
63     }
64 
getConnectedResId(Context context)65     private static int getConnectedResId(Context context) {
66         ConnectivityManager cm = (ConnectivityManager) context
67                 .getSystemService(Context.CONNECTIVITY_SERVICE);
68         NetworkInfo info = cm.getActiveNetworkInfo();
69 
70         int resId = 0;
71         if (info == null || !info.isAvailable() || !info.isConnected()) {
72             // can't have Internet access with no network
73             writeConnectivity(context, false);
74             resId = R.drawable.ic_widget_wifi_not_connected;
75         } else if (!readConnectivity(context)) {
76             resId = R.drawable.ic_widget_wifi_no_internet;
77         } else {
78             // internet is connected and working, show nothing
79             resId = android.R.color.transparent;
80         }
81 
82         return resId;
83     }
84 
writeConnectivity(Context context, boolean inetConnected)85     private static void writeConnectivity(Context context, boolean inetConnected) {
86         context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE).edit()
87                 .putBoolean(EXTRA_INET_CONDITION, inetConnected).apply();
88     }
89 
readConnectivity(Context context)90     private static boolean readConnectivity(Context context) {
91         return context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE).getBoolean(
92                 EXTRA_INET_CONDITION, true);
93     }
94 
95 }
96