@Override
  public void onReceive(Context context, Intent intent) {
    Log.d(LOG_TAG, "Receiver called");
    String action = intent.getAction();
    if (action == null) {
      return;
    }

    Log.v(LOG_TAG, "Receiver called with action: " + action);
    if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
      syncChecking(context);
    } else if (action.equals(Veecheck.getRescheduleAction(context))) {
      syncChecking(context);
    } else if (action.equals(Veecheck.getConsiderAction(context))) {
      considerChecking(context);
    }
  }
Example #2
0
  /** On application creation. */
  @Override
  public void onCreate() {
    super.onCreate();
    sInstance = this;

    Connection.setContext(getApplicationContext());

    // Error Reporter
    CustomExceptionHandler customExceptionHandler = CustomExceptionHandler.getInstance();
    customExceptionHandler.Init(sInstance.getApplicationContext());
    Thread.setDefaultUncaughtExceptionHandler(customExceptionHandler);

    SharedPreferences preferences = PrefSettings.getSharedPrefs(this);
    // Assign some default settings if necessary
    if (preferences.getString(PrefSettings.KEY_CHECK_URI, null) == null) {
      Editor editor = preferences.edit();
      // Test Update Notifications
      // Some ridiculously fast polling, just to demonstrate it working...
      /*
       * editor.putBoolean(PrefSettings.KEY_ENABLED, true); editor.putLong(PrefSettings.KEY_PERIOD, 30 * 1000L);
       * editor.putLong(PrefSettings.KEY_CHECK_INTERVAL, 60 * 1000L); editor.putString(PrefSettings.KEY_CHECK_URI,
       * "http://ankidroid.googlecode.com/files/test_notifications.xml");
       */
      editor.putString(
          PrefSettings.KEY_CHECK_URI, "http://ankidroid.googlecode.com/files/last_release.xml");

      // Create the folder "AnkiDroid", if not exists, where the decks
      // will be stored by default
      new File(getStorageDirectory() + "/AnkiDroid").mkdir();

      // Put the base path in preferences pointing to the default
      // "AnkiDroid" folder
      editor.putString("deckPath", getStorageDirectory() + "/AnkiDroid");

      // Using commit instead of apply even though we don't need a return value.
      // Reason: apply() not available on Android 1.5
      editor.commit();
    }

    // Reschedule the checks - we need to do this if the settings have
    // changed (as above)
    // It may also necessary in the case where an application has been
    // updated
    // Here for simplicity, we do it every time the application is launched
    Intent intent = new Intent(Veecheck.getRescheduleAction(this));
    sendBroadcast(intent);
  }
  /**
   * Consults the settings to determine whether a {@link Service} should be started to perform a
   * check for application updates.
   *
   * @param context the context in which the {@link BroadcastReceiver} is operating
   */
  private void considerChecking(Context context) {
    Log.d(LOG_TAG, "Considering performing check.");
    VeecheckSettings settings = createSettings(context);
    if (!settings.isEnabled()) {
      syncChecking(context);
      return;
    }
    Log.d(LOG_TAG, "Checking is enabled.");

    String uri = settings.getCheckUri();
    if (uri == null) {
      return; // no point continuing - not configured
    }
    Log.d(LOG_TAG, "URI is available.");

    VeecheckState state = createState(context);
    long now = System.currentTimeMillis();
    long lastCheck = state.getLastCheck();
    if (lastCheck >= 0L && lastCheck + settings.getCheckInterval() > now) {
      return; // it has run too recently
    }
    Log.d(LOG_TAG, "Last check was not too recent.");

    // TODO get battery status: not possible yet, see:
    // http://code.google.com/p/android/issues/detail?id=926
    int status = BatteryManager.BATTERY_STATUS_CHARGING;
    if (status != BatteryManager.BATTERY_STATUS_CHARGING) {
      return; // run some other, better, time
    }
    Log.d(LOG_TAG, "Battery is in a suitable state.");

    // attach our package as an extra on the intent
    Intent intent = new Intent(Veecheck.getCheckAction(context), Uri.parse(uri));
    context.startService(intent);
    // Note: this is an awkward part of the design...
    // It might seem ideal to have this called by the Service only when a
    // check has been successfully made, but this is not obviously the right
    // design.
    state.setLastCheckNow(now);
    Log.d(LOG_TAG, "Last check date was updated.");
  }
 /**
  * Constructs a {@link PendingIntent} that will be broadcast by the {@link AlarmManager} to prompt
  * this {@link BroadcastReceiver} to consider checking for updates.
  *
  * @param context the context in which the {@link BroadcastReceiver} is operating
  * @return an intent, pending to {@link Veecheck#getConsiderAction(Context)}
  */
 private PendingIntent createCheckingIntent(Context context) {
   return PendingIntent.getBroadcast(
       context, 0, new Intent(Veecheck.getConsiderAction(context)), 0);
 }