kalehv
11/20/2016 - 7:38 PM

AndroidManifest.xml

package me.kalehv.widget;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Binder;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

/**
 * Created by Harshad Kale on 11/19/16.
 */

public class WidgetRemoteViewsService extends RemoteViewsService {

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return (new WidgetListAdapter(getApplicationContext(), intent));
    }

    class WidgetListAdapter implements RemoteViewsService.RemoteViewsFactory {

        private final String TAG = WidgetListAdapter.class.getSimpleName();

        private Cursor mCursor;
        private Context mContext;
        private int mAppWidgetId;

        public WidgetListAdapter(Context context, Intent intent) {
            mContext = context;
            mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        @Override
        public void onCreate() {

        }

        @Override
        public void onDataSetChanged() {
            Log.d(TAG, "onDataSetChanged: ");

            if (mCursor != null) {
                mCursor.close();
            }

            final long token = Binder.clearCallingIdentity();

            try {
                mCursor = mContext.getContentResolver().query(ClipProvider.Clips.CONTENT_URI, null, null, null, null);
            } finally {
                Binder.restoreCallingIdentity(token);
            }
        }

        @Override
        public void onDestroy() {
            Log.d(TAG, "onDestroy: ");

            if (mCursor != null) {
                mCursor.close();
            }
        }

        @Override
        public int getCount() {
            Log.d(TAG, "getCount: ");

            return mCursor.getCount();
        }

        @Override
        public RemoteViews getViewAt(int position) {
            Log.d(TAG, "getViewAt: " + position);

            String text = "";
            int id = -1;

            if (mCursor.moveToPosition(position)) {
                id = mCursor.getInt(mCursor.getColumnIndex(ClipColumns._ID));
                text = mCursor.getString(mCursor.getColumnIndex(ClipColumns.TEXT));
            }

            RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.item_list_clip);
            rv.setTextViewText(R.id.clip_text_textview, text);

            // Set the click intent so that we can handle it and show a toast message
            Bundle extras = new Bundle();
            extras.putInt(WidgetProvider.WIDGET_ITEM_CLICK_ID, id);
            extras.putString(WidgetProvider.WIDGET_ITEM_CLICK_TEXT, text);

            Intent fillInIntent = new Intent();
            fillInIntent.putExtras(extras);
            rv.setOnClickFillInIntent(R.id.clip_text_textview, fillInIntent);

            return rv;
        }

        @Override
        public RemoteViews getLoadingView() {
            Log.d(TAG, "getLoadingView: ");

            return null;
        }

        @Override
        public int getViewTypeCount() {
            Log.d(TAG, "getViewTypeCount: ");

            return 1;
        }

        @Override
        public long getItemId(int position) {
            Log.d(TAG, "getItemId: ");

            return position;
        }

        @Override
        public boolean hasStableIds() {
            Log.d(TAG, "hasStableIds: ");
            
            return true;
        }
    }
}
package me.kalehv.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;

/**
 * Created by harshadkale on 11/18/16.
 */

public class WidgetProvider extends AppWidgetProvider {

    private final String TAG = WidgetProvider.class.getSimpleName();

    public static String WIDGET_ITEM_CLICK_ACTION = "me.kalehv.widget.CLICK";
    public static String WIDGET_ITEM_CLICK_ID = "me.kalehv.widget.CLICK_ID";
    public static String WIDGET_ITEM_CLICK_TEXT = "me.kalehv.widget.CLICK_TEXT";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive: ");

        String action = intent.getAction();
        if (action.equals(WIDGET_ITEM_CLICK_ACTION)) {
            String text = intent.getStringExtra(WIDGET_ITEM_CLICK_TEXT);
            int id = intent.getIntExtra(WIDGET_ITEM_CLICK_ID, -1);

            if (id != -1) {
                Intent serviceIntent = new Intent(context, SyncService.class);
                serviceIntent.setAction(C.MY_ACTION);
                serviceIntent.putExtra(ClipColumns._ID, id);
                serviceIntent.putExtra(ClipColumns.TEXT, text);
                context.startService(intent);
            }
        }

        super.onReceive(context, intent);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.d(TAG, "onUpdate: ");

        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_listview);
        for (int id : appWidgetIds) {
            RemoteViews remoteViews = updateWidgetListView(context, id);
            appWidgetManager.updateAppWidget(id, remoteViews);
        }

        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
        Log.d(TAG, "updateWidgetListView: ");

        Intent intent = new Intent(context, WidgetRemoteViewsService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

        //which layout to show on widget
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        remoteViews.setRemoteAdapter(R.id.widget_listview, intent);
        remoteViews.setEmptyView(R.id.widget_listview, R.id.empty_widget_list_textview);

        final Intent onClickIntent = new Intent(context, WidgetProvider.class);
        onClickIntent.setAction(WidgetProvider.WIDGET_ITEM_CLICK_ACTION);
        onClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        onClickIntent.setData(Uri.parse(onClickIntent.toUri(Intent.URI_INTENT_SCHEME)));
        final PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0,
                onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setPendingIntentTemplate(R.id.widget_listview, onClickPendingIntent);

        return remoteViews;
    }
}
<!--Widget-->
<receiver
        android:name=".widget.WidgetProvider"
        android:label="My Widget"
        android:icon="@mipmap/ic_launcher">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
        <action android:name="me.kalehv.widget.CLICK"/> <!-- Not sure if this is needed but it doesn't work regardless-->
    </intent-filter>
    <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_info"/>
</receiver>
<service
        android:name=".widget.WidgetRemoteViewsService"
        android:permission="android.permission.BIND_REMOTEVIEWS"/>