Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a

PendingIntent templatePendingIntent = PendingIntent.getActivity(
context,
0,
appWidgetId,
templateIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFactory {
private Context mContext;
private int mAppWidgetId;
private NoteSQLiteOpenHelper db;
private DBNote note;
private static final String TAG = SingleNoteWidget.class.getSimpleName();

public SingleNoteWidgetFactory(Context context, Intent intent) {
Expand All @@ -28,14 +30,15 @@ public SingleNoteWidgetFactory(Context context, Intent intent) {

@Override
public void onCreate() {

db = NoteSQLiteOpenHelper.getInstance(mContext);
}

@Override
public boolean hasStableIds() {
return true;
}

// TODO Set loading view
@Override
public RemoteViews getLoadingView() {
return null;
Expand All @@ -48,57 +51,57 @@ public int getViewTypeCount() {

@Override
public void onDataSetChanged() {
SharedPreferences sharedprefs = PreferenceManager.getDefaultSharedPreferences(mContext);
long noteID = sharedprefs.getLong(SingleNoteWidget.WIDGET_KEY + mAppWidgetId, -1);

if (noteID >= 0) {
if (db.getNote(noteID) == null) {
Log.e(TAG, "Error: note not found");
note = null;
} else {
note = db.getNote(noteID);
}
}
}

@Override
public void onDestroy() {

db.close();
}

/**
* getCount() always runs 1 because the list view only ever has a
* single note displayed
* @return
* Returns the number of items in the data set. In this case, always 1 as a single note is
* being displayed. Will return 0 when the note can't be displayed.
*
*/
@Override
public int getCount() {
return 1;
return (note != null) ? 1 : 0;
}

/**
* getViewAt - Returns a RemoteView containing the note content in a TextView and
* a fillInIntent to handle the user tapping on the item in the list
* view.
* Returns a RemoteView containing the note content in a TextView and
* a fillInIntent to handle the user tapping on the item in the list view.
*
* @param position The position of the item in the list
* @return The RemoteView at the specified position in the list
*/
@Override
public RemoteViews getViewAt(int position) {
if (note == null) {
return null;
}

RemoteViews note_content = new RemoteViews(mContext.getPackageName(),
R.layout.widget_single_note_content);

SharedPreferences sharedprefs = PreferenceManager.getDefaultSharedPreferences(mContext);
long noteID = sharedprefs.getLong(SingleNoteWidget.WIDGET_KEY + mAppWidgetId, -1);

if (noteID >= 0) {
NoteSQLiteOpenHelper db = NoteSQLiteOpenHelper.getInstance(mContext);
DBNote note = db.getNote(noteID);

final Intent fillInIntent = new Intent();
final Bundle extras = new Bundle();

extras.putSerializable(EditNoteActivity.PARAM_NOTE, note);

fillInIntent.putExtras(extras);
fillInIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
note_content.setOnClickFillInIntent(R.id.single_note_content_tv, fillInIntent);
note_content.setTextViewText(R.id.single_note_content_tv, note.getContent());
} else {
Log.e(TAG, "Note not found");
note_content.setTextViewText(R.id.single_note_content_tv, "Note not found");
}
final Intent fillInIntent = new Intent();
final Bundle extras = new Bundle();

extras.putSerializable(EditNoteActivity.PARAM_NOTE, note);
fillInIntent.putExtras(extras);
fillInIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
note_content.setOnClickFillInIntent(R.id.single_note_content_tv, fillInIntent);
note_content.setTextViewText(R.id.single_note_content_tv, note.getContent());

return note_content;
}
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/res/layout/widget_single_note.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@
android:id="@+id/widget_single_note"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/widget_background"
>

<ListView
android:id="@+id/single_note_widget_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/widget_background"
android:padding="@dimen/widget_single_note_padding"
/>

<TextView
android:id="@+id/widget_single_note_placeholder_tv"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"/>
android:gravity="center"
android:text="@string/widget_single_note_placeholder_tv"
android:textColor="@color/fg_default_high"
android:textAlignment="center"
android:padding="@dimen/widget_single_note_padding"
/>

</RelativeLayout>
<!--
TODO: Markdown support
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/widget_single_note_content.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
android:id="@+id/single_note_content_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/fg_default"
/>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<!-- Widgets -->
<string name="widget_note_list_title">Note list</string>
<string name="widget_single_note_title">Single note</string>
<string name="widget_single_note_placeholder_tv">Note not found</string>
<string name="widget_create_note">Create Note</string>
<string name="widget_not_logged_in">Please log in to Notes before using this widget</string>

Expand Down