Skip to content

Commit

Permalink
Fixed bugs: widget was not updating directly after creating/ending, f…
Browse files Browse the repository at this point in the history
…ixed bug where clicking on widget would not redirect to the right place
  • Loading branch information
lubenard committed Jun 6, 2021
1 parent a00c539 commit e44155f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class CurrentSessionWidgetProvider extends AppWidgetProvider {
private static RemoteViews remoteViews;

public static String WIDGET_BUTTON = "com.lubenard.oring_reminder.WIDGET_BUTTON";
private static final String TAG = "Widget";

public static boolean isThereAWidget = false;
private static AlarmManager am;
Expand All @@ -40,6 +41,8 @@ public class CurrentSessionWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;

Log.d(TAG, "Updating widget");

// Perform this loop procedure for each App Widget that belongs to this provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
Expand All @@ -56,6 +59,7 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
RingModel lastEntry = dbManager.getLastRunningEntry();

if (lastEntry != null) {
Log.d(TAG, "A current session has been found");
// Set the 'Create session' button to invisible
remoteViews.setViewVisibility(R.id.widget_button_new_session, View.GONE);

Expand Down Expand Up @@ -85,6 +89,7 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a

intent.putExtra("switchToEntry", lastEntry.getId());
} else {
Log.d(TAG, "There is no current session");
remoteViews.setTextViewText(R.id.widget_date_from, "");
remoteViews.setTextViewText(R.id.widget_worn_for, context.getString(R.string.no_running_session));
remoteViews.setViewVisibility(R.id.widget_button_new_session, View.VISIBLE);
Expand All @@ -97,7 +102,7 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
remoteViews.setOnClickPendingIntent(R.id.widget_button_new_session, pendingIntent2 );
}

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widget_root_view, pendingIntent);

// Update the widget view.
Expand All @@ -109,7 +114,7 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
public void onEnabled(Context context) {
super.onEnabled(context);
isThereAWidget = true;
Log.d("Widget", "onEnabled is called");
Log.d(TAG, "onEnabled is called");
dbManager = new DbManager(context);
Intent intent = new Intent(context, CurrentSessionWidgetProvider.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, 0);
Expand All @@ -122,7 +127,7 @@ public void onEnabled(Context context) {
public void onDisabled(Context context) {
super.onDisabled(context);
isThereAWidget = false;
Log.d("Widget", "onDisabled is called");
Log.d(TAG, "onDisabled is called");
Intent intent = new Intent(context, CurrentSessionWidgetProvider.class);
PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 1, intent, 0);
// Cancel alarm manager
Expand All @@ -134,9 +139,9 @@ public void onDisabled(Context context) {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.d("Widget", "Widget receives OnRecieve command to update");
Log.d(TAG, "Widget receives OnRecieve command to update");

Log.d("Widget", "intent action is " + intent.getAction());
Log.d(TAG, "intent action is " + intent.getAction());
if (WIDGET_BUTTON.equals(intent.getAction())) {
EditEntryFragment.setUpdateMainList(false);
new EditEntryFragment(context).insertNewEntry(Utils.getdateFormatted(new Date()), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import androidx.appcompat.app.AppCompatActivity;
Expand Down Expand Up @@ -137,12 +138,14 @@ public void onCreate(Bundle savedInstanceState) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Intent intent = getIntent();
if (intent.getLongExtra("switchToEntry", -1) != -1) {
Log.d("Widget", "Opening for given session id : " + intent.getLongExtra("switchToEntry", -1));
Bundle bundle = new Bundle();
bundle.putLong("entryId", intent.getLongExtra("switchToEntry", -1));
Fragment fragment = new EntryDetailsFragment();
fragment.setArguments(bundle);
fragmentTransaction.replace(android.R.id.content, fragment);
} else {
Log.d("Widget", "No given session id");
// Then switch to the main Fragment
fragmentTransaction.replace(android.R.id.content, new MainFragment());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,11 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
* @param context
*/
public static void updateWidget(Context context) {
if (CurrentSessionWidgetProvider.isThereAWidget) {
//if (CurrentSessionWidgetProvider.isThereAWidget) {
Log.d(TAG, "Updating Widget");
Intent intent = new Intent(context, CurrentSessionWidgetProvider.class);
context.sendBroadcast(intent);
}
//}
}

/**
Expand Down

0 comments on commit e44155f

Please sign in to comment.