Skip to content
Snippets Groups Projects
Commit 3e35f388 authored by Jawnnypoo's avatar Jawnnypoo
Browse files

Fix up the widget a bit so that we show the accounts and users choose between them

parent 63aec8b0
No related branches found
No related tags found
No related merge requests found
Showing
with 307 additions and 66 deletions
Loading
Loading
@@ -81,7 +81,13 @@
</intent-filter>
</activity>
 
<receiver android:name=".widget.StackWidgetProvider"
<activity android:name=".widget.FeedWidgetConfigureActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<receiver android:name=".widget.FeedWidgetProvider"
android:label="GitLab Feed">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
Loading
Loading
@@ -91,7 +97,7 @@
</receiver>
 
<service
android:name=".widget.StackWidgetService"
android:name=".widget.FeedWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
 
</application>
Loading
Loading
Loading
Loading
@@ -46,6 +46,7 @@ public class Prefs {
return LoganSquare.parseList(accountsJson, Account.class);
} catch (IOException e) {
//why would this ever happen?!?!?1
getSharedPrefs(context).edit().remove(KEY_ACCOUNTS).commit();
}
return new ArrayList<>();
} else {
Loading
Loading
Loading
Loading
@@ -3,8 +3,7 @@ package com.commit451.gitlab.widget;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.graphics.Bitmap;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
 
Loading
Loading
@@ -14,6 +13,8 @@ import com.commit451.gitlab.api.GitLabRss;
import com.commit451.gitlab.model.Account;
import com.commit451.gitlab.model.rss.Entry;
import com.commit451.gitlab.model.rss.Feed;
import com.commit451.gitlab.transformation.CircleTransformation;
import com.squareup.picasso.Picasso;
 
import java.io.IOException;
import java.util.ArrayList;
Loading
Loading
@@ -23,15 +24,14 @@ import retrofit2.Response;
/**
* Remote all the views
*/
public class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
public class FeedRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
 
private static final int mCount = 10;
private Context mContext;
private int mAppWidgetId;
private GitLabRss mApi;
private ArrayList<Entry> mEntries;
 
public StackRemoteViewsFactory(Context context, Intent intent) {
public FeedRemoteViewsFactory(Context context, Intent intent) {
mContext = context;
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
Loading
Loading
@@ -40,11 +40,6 @@ public class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFa
@Override
public void onCreate() {
mEntries = new ArrayList<>();
//TODO remove, remove, remove
Account account = new Account();
account.setServerUrl(Uri.parse("https://gitlab.com/"));
account.setPrivateToken("yyvWMKRW6DHxLVK6nzeF");
mApi = GitLabClient.rssInstance(account);
}
 
@Override
Loading
Loading
@@ -70,25 +65,21 @@ public class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFa
rv.setTextViewText(R.id.summary, entry.getSummary());
 
// Next, we set a fill-intent which will be used to fill-in the pending intent template
// which is set on the collection view in StackWidgetProvider.
Bundle extras = new Bundle();
extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
// which is set on the collection view in FeedWidgetProvider.
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
fillInIntent.putExtra(FeedWidgetProvider.EXTRA_LINK, entry.getLink().getHref().toString());
rv.setOnClickFillInIntent(R.id.root, fillInIntent);
 
//Does not like that these do not come from the main thread
// Picasso.with(mContext)
// .load(entry.getThumbnail().getUrl())
// .into(rv, R.id.image, new int[] { mAppWidgetId });
// You can do heaving lifting in here, synchronously. For example, if you need to
// process an image, fetch something from the network, etc., it is ok to do it here,
// synchronously. A loading view will show up in lieu of the actual contents in the
// interim.
try {
Bitmap image = Picasso.with(mContext)
.load(entry.getThumbnail().getUrl())
.transform(new CircleTransformation())
.get();
rv.setImageViewBitmap(R.id.image, image);
} catch (IOException e) {
//well, thats too bad
}
 
// Return the remote views object.
return rv;
}
 
Loading
Loading
@@ -122,9 +113,14 @@ public class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFa
// from the network, etc., it is ok to do it here, synchronously. The widget will remain
// in its current state while work is being done here, so you don't need to worry about
// locking up the widget.
//TODO unhardcode this
Account account = FeedWidgetPrefs.getAccount(mContext, mAppWidgetId);
if (account == null || account.getUser() == null || account.getUser().getFeedUrl() == null) {
//TODO show error state?
return;
}
GitLabRss rssClient = GitLabClient.rssInstance(account);
try {
Response<Feed> feedResponse = mApi.getFeed("https://gitlab.com/Commit451/LabCoat.atom").execute();
Response<Feed> feedResponse = rssClient.getFeed(account.getUser().getFeedUrl().toString()).execute();
if (feedResponse.isSuccessful()) {
if (feedResponse.body().getEntries() != null) {
mEntries.addAll(feedResponse.body().getEntries());
Loading
Loading
package com.commit451.gitlab.widget;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import com.commit451.gitlab.R;
import com.commit451.gitlab.activity.BaseActivity;
import com.commit451.gitlab.adapter.AccountsAdapter;
import com.commit451.gitlab.data.Prefs;
import com.commit451.gitlab.model.Account;
import java.util.Collections;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import timber.log.Timber;
/**
* The configuration screen for the ExampleAppWidgetProvider widget sample.
*/
public class FeedWidgetConfigureActivity extends BaseActivity {
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
@BindView(R.id.toolbar)
Toolbar mToolbar;
@BindView(R.id.message_text)
TextView mTextMessage;
@BindView(R.id.list)
RecyclerView mList;
AccountsAdapter mAccountAdapter;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setResult(RESULT_CANCELED);
setContentView(R.layout.activity_feed_widget_configure);
ButterKnife.bind(this);
// 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 they gave us an intent without the widget id, just bail.
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
mToolbar.setTitle(R.string.widget_choose_account);
mAccountAdapter = new AccountsAdapter(this, new AccountsAdapter.Listener() {
@Override
public void onAccountClicked(Account account) {
saveWidgetConfig(account);
}
@Override
public void onAddAccountClicked() {
}
@Override
public void onAccountLogoutClicked(Account account) {
}
});
mList.setLayoutManager(new LinearLayoutManager(this));
mList.setAdapter(mAccountAdapter);
loadAccounts();
}
private void loadAccounts() {
List<Account> accounts = Prefs.getAccounts(this);
Timber.d("Got %s accounts", accounts.size());
Collections.sort(accounts);
Collections.reverse(accounts);
if (accounts.isEmpty()) {
mTextMessage.setVisibility(View.VISIBLE);
} else {
mTextMessage.setVisibility(View.GONE);
mAccountAdapter.setAccounts(accounts);
}
}
private void saveWidgetConfig(Account account) {
FeedWidgetPrefs.setAccount(FeedWidgetConfigureActivity.this, mAppWidgetId, account);
// Push widget update to surface with newly set prefix
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(FeedWidgetConfigureActivity.this);
// ExampleAppWidgetProvider.updateAppWidget(context, appWidgetManager,
// mAppWidgetId, titlePrefix);
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
}
package com.commit451.gitlab.widget;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.bluelinelabs.logansquare.LoganSquare;
import com.commit451.gitlab.model.Account;
import java.io.IOException;
/**
* The prefs for the feed widget
*/
public class FeedWidgetPrefs {
public static String FILE_NAME = "LabCoatWidgetPrefs";
private static final String KEY_ACCOUNT = "_account";
private static SharedPreferences getSharedPrefs(Context context) {
return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
}
@Nullable
public static Account getAccount(Context context, int widgetId) {
String accountsJson = getSharedPrefs(context).getString(widgetId + KEY_ACCOUNT, null);
if (!TextUtils.isEmpty(accountsJson)) {
try {
return LoganSquare.parse(accountsJson, Account.class);
} catch (IOException e) {
//why would this ever happen?!?!?1
getSharedPrefs(context).edit().remove(widgetId + KEY_ACCOUNT).commit();
}
}
return null;
}
public static void setAccount(Context context, int widgetId, Account account) {
try {
String json = LoganSquare.serialize(account);
getSharedPrefs(context)
.edit()
.putString(widgetId + KEY_ACCOUNT, json)
.commit();
} catch (IOException e) {
//this wont happen! Right?!?!?!
}
}
}
Loading
Loading
@@ -23,58 +23,57 @@ import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;
import android.widget.Toast;
 
import com.commit451.gitlab.R;
import com.commit451.gitlab.navigation.DeepLinker;
 
public class StackWidgetProvider extends AppWidgetProvider {
public static final String TOAST_ACTION = "com.example.android.stackwidget.TOAST_ACTION";
public static final String EXTRA_ITEM = "com.example.android.stackwidget.EXTRA_ITEM";
public class FeedWidgetProvider extends AppWidgetProvider {
public static final String ACTION_FOLLOW_LINK = "com.commit451.gitlab.ACTION_FOLLOW_LINK";
public static final String EXTRA_LINK = "com.commit451.gitlab.EXTRA_LINK";
 
@Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
if (intent.getAction().equals(TOAST_ACTION)) {
if (intent.getAction().equals(ACTION_FOLLOW_LINK)) {
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0);
Toast.makeText(context, "Touched view " + viewIndex, Toast.LENGTH_SHORT).show();
String uri = intent.getStringExtra(EXTRA_LINK);
Intent launchIntent = DeepLinker.generateDeeplinkIntentFromUri(context, Uri.parse(uri));
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(launchIntent);
}
super.onReceive(context, intent);
}
 
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// update each of the widgets with the remote adapter
for (int i = 0; i < appWidgetIds.length; ++i) {
for (int widgetId : appWidgetIds) {
 
// Here we setup the intent which points to the StackViewService which will
// provide the views for this collection.
Intent intent = new Intent(context, StackWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
Intent intent = new Intent(context, FeedWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout_entry);
rv.setRemoteAdapter(R.id.list_view, intent);
 
// The empty view is displayed when the collection has no items. It should be a sibling
// of the collection view.
rv.setEmptyView(R.id.list_view, R.id.empty_view);
 
// Here we setup the a pending intent template. Individuals items of a collection
// cannot setup their own pending intents, instead, the collection as a whole can
// setup a pending intent template, and the individual items can set a fillInIntent
// to create unique before on an item to item basis.
Intent toastIntent = new Intent(context, StackWidgetProvider.class);
toastIntent.setAction(StackWidgetProvider.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
Intent toastIntent = new Intent(context, FeedWidgetProvider.class);
toastIntent.setAction(FeedWidgetProvider.ACTION_FOLLOW_LINK);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.list_view, toastPendingIntent);
 
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
appWidgetManager.updateAppWidget(widgetId, rv);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
Loading
Loading
Loading
Loading
@@ -22,9 +22,9 @@ import android.widget.RemoteViewsService;
/**
* Service that basically just defers everything to a Factory. Yay!
*/
public class StackWidgetService extends RemoteViewsService {
public class FeedWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new StackRemoteViewsFactory(getApplicationContext(), intent);
return new FeedRemoteViewsFactory(getApplicationContext(), intent);
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.commit451.gitlab.view.LabCoatSwipeRefreshLayout
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</com.commit451.gitlab.view.LabCoatSwipeRefreshLayout>
<TextView
android:id="@+id/message_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:text="@string/widget_config_no_accounts"
android:gravity="center"
android:layout_gravity="center"
android:visibility="gone"/>
</FrameLayout>
</LinearLayout>
\ No newline at end of file
Loading
Loading
@@ -5,8 +5,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:orientation="horizontal"
android:id="@+id/root">
Loading
Loading
@@ -32,7 +32,6 @@
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="16sp"
tools:text="Project" />
 
Loading
Loading
@@ -40,7 +39,6 @@
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="14sp"
tools:text="I like this project because its called a project and it has a really long description that should cut off after a few lines" />
 
Loading
Loading
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_blue_60">
android:orientation="vertical">
 
<ListView
android:id="@+id/list_view"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="48dp"
android:background="@color/main_blue">
 
<TextView
android:id="@+id/empty_view"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textColor="@color/white"
android:textSize="18sp"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:text="@string/widget_feed"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Loading activity..."
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold" />
</FrameLayout>
\ No newline at end of file
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/main_blue_60">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
</LinearLayout>
Loading
Loading
@@ -407,4 +407,10 @@
<string name="deeplink_path_issue">issue</string>
 
<string name="deeplink_navigate_error">No idea where to navigate to</string>
<!-- Widget -->
<string name="widget_feed">Feed</string>
<string name="widget_loading">Loading&#8230;</string>
<string name="widget_config_no_accounts">No accounts</string>
<string name="widget_choose_account">Choose Account</string>
</resources>
\ No newline at end of file
Loading
Loading
@@ -7,4 +7,5 @@
android:previewImage="@mipmap/ic_launcher"
android:initialLayout="@layout/widget_layout_entry"
android:resizeMode="horizontal|vertical"
android:configure="com.commit451.gitlab.widget.FeedWidgetConfigureActivity"
android:widgetCategory="home_screen"/>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment