// Called when plugin is turned off
  @Override
  public void onDestroy() {
    super.onDestroy();

    Aware.stopPlugin(this, pkg_google_fused);
    Aware.stopPlugin(this, pkg_google_activity_recog);
    Aware.setSetting(
        this, "status_plugin_google_activity_recognition", false, pkg_google_activity_recog);
    Aware.setSetting(this, "status_google_fused_location", false, pkg_google_fused);
    Aware.setSetting(this, Aware_Preferences.STATUS_INSTALLATIONS, false);
    //   Aware.setSetting(this, Aware_Preferences.STATUS_ACCELEROMETER, false);

    if (activityObs != null) {
      getContentResolver().unregisterContentObserver(activityObs);
    }

    sendBroadcast(new Intent(Aware.ACTION_AWARE_REFRESH));
  }
  // Called once, when your plugin is started
  @Override
  public void onCreate() {
    super.onCreate();

    prefs = getSharedPreferences(getPackageName(), MODE_PRIVATE);

    Intent aware = new Intent(this, Aware.class);
    startService(aware);

    if (!prefs.contains("device_id")) {
      UUID uuid = UUID.randomUUID();

      ContentValues newId = new ContentValues();
      newId.put(Aware_Provider.Aware_Settings.SETTING_KEY, Aware_Preferences.DEVICE_ID);
      newId.put(Aware_Provider.Aware_Settings.SETTING_VALUE, uuid.toString());
      newId.put(Aware_Provider.Aware_Settings.SETTING_PACKAGE_NAME, "com.aware");
      getContentResolver().insert(Aware_Provider.Aware_Settings.CONTENT_URI, newId);

      SharedPreferences.Editor editor = prefs.edit();
      editor.putString("device_id", uuid.toString());
      editor.apply();
    }

    // Turn debugging messages on
    Aware.setSetting(this, Aware_Preferences.DEBUG_FLAG, true);

    TAG = "HCII::TRACKER";
    DEBUG = Aware.getSetting(this, Aware_Preferences.DEBUG_FLAG).equals("true");

    // Start plugins first, before setting settings for them. If the phone doesn't have them,
    // download will be requested. Once installed, AWARE will automatically start them.
    Aware.startPlugin(this, pkg_google_fused);
    Aware.startPlugin(this, pkg_google_activity_recog);

    // Activate installations sensor
    Aware.setSetting(this, Aware_Preferences.STATUS_INSTALLATIONS, true);

    // Activate Accelerometer
    //  Aware.setSetting(this, Aware_Preferences.STATUS_ACCELEROMETER, true);

    // Set sampling frequency
    //  Aware.setSetting(this, Aware_Preferences.FREQUENCY_ACCELEROMETER, 200000); //this is too
    // fast! This is a millisecond delay between samples. See documentation of what values it
    // expects!

    // Set Google Activity Recognition settings - see online documentation for setting values
    Aware.setSetting(
        this, "status_plugin_google_activity_recognition", true, pkg_google_activity_recog);
    Aware.setSetting(
        this, "frequency_plugin_google_activity_recognition", 60, pkg_google_activity_recog);

    // Set Google Fused Location settings - See online documentation for settings values
    Aware.setSetting(this, "status_google_fused_location", true, pkg_google_fused);
    Aware.setSetting(this, "frequency_google_fused_location", 60, pkg_google_fused);
    Aware.setSetting(this, "max_frequency_google_fused_location", 60, pkg_google_fused);
    Aware.setSetting(this, "accuracy_google_fused_location", 102, pkg_google_fused);
    //  Aware.setSetting(this, Aware_Preferences.STATUS_WEBSERVICE, true);
    //   Aware.setSetting(this, Aware_Preferences.WEBSERVICE_SERVER., “127.0.0.1”);

    // Apply settings again
    Aware.startPlugin(getApplicationContext(), pkg_google_fused);
    Aware.startPlugin(getApplicationContext(), pkg_google_activity_recog);

    ACTIVITY_URI =
        Uri.parse(
            "content://com.aware.plugin.google.activity_recognition.provider/plugin_google_activity_recognition");
    activityObs = new ActivityRecognitionObserver(new Handler());
    getContentResolver().registerContentObserver(ACTIVITY_URI, true, activityObs);

    sendBroadcast(new Intent(Aware.ACTION_AWARE_REFRESH));
    /*
    if( DATABASE_TABLES != null && TABLES_FIELDS != null && CONTEXT_URIS != null) {
        for (int i = 0; i < DATABASE_TABLES.length; i++) {
            Intent webserviceHelper = new Intent(this, WebserviceHelper.class);
            webserviceHelper.setAction(WebserviceHelper.ACTION_AWARE_WEBSERVICE_SYNC_TABLE);
            webserviceHelper.putExtra(WebserviceHelper.EXTRA_TABLE, DATABASE_TABLES[i]);
            webserviceHelper.putExtra(WebserviceHelper.EXTRA_FIELDS, TABLES_FIELDS[i]);
            webserviceHelper.putExtra(WebserviceHelper.EXTRA_CONTENT_URI, CONTEXT_URIS[i].toString());
            this.startService(webserviceHelper);
        }
    }else {
        if( Aware.DEBUG ) Log.d(TAG,"No database to backup!");
    }
    */

  }