Ejemplo n.º 1
0
 @Override
 public void onCreate() {
   super.onCreate();
   SharedPreferences prefs =
       AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext());
   String path = prefs.getString("lastWidgetDeck", "");
   if (path != null && path.length() > 0 && AnkiDroidApp.isSdCardMounted()) {
     Log.i(AnkiDroidApp.TAG, "BigWidget: reloading deck " + path);
     mCol = Collection.currentCollection();
     if (mCol != null) {
       mCurrentCard = mCol.getSched().getCard();
     }
   }
 }
    private RemoteViews buildUpdate(Context context, boolean updateDueDecksNow) {
      Log.i(AnkiDroidApp.TAG, "buildUpdate");

      // Resources res = context.getResources();
      RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_small);

      // Add a click listener to open Anki from the icon.
      // This should be always there, whether there are due cards or not.
      Intent ankiDroidIntent = new Intent(context, StudyOptions.class);
      ankiDroidIntent.setAction(Intent.ACTION_MAIN);
      ankiDroidIntent.addCategory(Intent.CATEGORY_LAUNCHER);
      PendingIntent pendingAnkiDroidIntent =
          PendingIntent.getActivity(context, 0, ankiDroidIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      updateViews.setOnClickPendingIntent(
          R.id.ankidroid_widget_small_layout, pendingAnkiDroidIntent);

      boolean mounted = AnkiDroidApp.isSdCardMounted();
      if (!mounted) {
        updateViews.setViewVisibility(R.id.widget_due, View.INVISIBLE);
        updateViews.setViewVisibility(R.id.widget_eta, View.INVISIBLE);
        updateViews.setViewVisibility(R.id.widget_progress_frame, View.INVISIBLE);
        updateViews.setViewVisibility(R.id.ankidroid_widget_small_finish_layout, View.GONE);

        if (mMountReceiver == null) {
          mMountReceiver =
              new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                  String action = intent.getAction();
                  if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
                    Log.i(AnkiDroidApp.TAG, "mMountReceiver - Action = Media Mounted");
                    if (remounted) {
                      WidgetStatus.update(getBaseContext());
                      remounted = false;
                      if (mMountReceiver != null) {
                        unregisterReceiver(mMountReceiver);
                      }
                    } else {
                      remounted = true;
                    }
                  }
                }
              };
          IntentFilter iFilter = new IntentFilter();
          iFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
          iFilter.addDataScheme("file");
          registerReceiver(mMountReceiver, iFilter);
        }
      } else {
        // If we do not have a cached version, always update.
        if (dueCardsCount == 0 || updateDueDecksNow) {
          // Build a list of decks with due cards.
          // Also compute the total number of cards due.
          int[] counts = WidgetStatus.fetchSmall(context);

          dueCardsCount = counts[0];
          reps = counts[1];
          eta = counts[2];
          int totalreps = reps + dueCardsCount;
          int progress = 0;
          if (totalreps != 0) {
            progress = (int) Math.round((100.0d * reps) / totalreps);
          }
          if (dueCardsCount <= 0) {
            if (dueCardsCount == 0) {
              updateViews.setViewVisibility(
                  R.id.ankidroid_widget_small_finish_layout, View.VISIBLE);
            } else {
              updateViews.setViewVisibility(
                  R.id.ankidroid_widget_small_finish_layout, View.INVISIBLE);
            }
            updateViews.setViewVisibility(R.id.widget_due, View.INVISIBLE);
            updateViews.setViewVisibility(R.id.widget_progress_frame, View.INVISIBLE);
          } else {
            updateViews.setViewVisibility(
                R.id.ankidroid_widget_small_finish_layout, View.INVISIBLE);
            updateViews.setViewVisibility(R.id.widget_due, View.VISIBLE);
            updateViews.setViewVisibility(R.id.widget_progress_frame, View.VISIBLE);
            updateViews.setTextViewText(R.id.widget_due, Integer.toString(dueCardsCount));
            updateViews.setProgressBar(R.id.widget_progress, 100, progress, false);
          }
          if (eta <= 0 || dueCardsCount <= 0) {
            updateViews.setViewVisibility(R.id.widget_eta, View.INVISIBLE);
          } else {
            updateViews.setViewVisibility(R.id.widget_eta, View.VISIBLE);
            updateViews.setTextViewText(R.id.widget_eta, Integer.toString(eta));
          }
        }
      }

      return updateViews;
    }