Exemplo n.º 1
0
  @Override
  protected void onHandleIntent(Intent intent) {
    Log.e(TAG, "getting notifications. . . ");
    String username = intent.getStringExtra(Constants.User.USERNAME);
    TBSUserInterface service = ServiceManager.getInstance();
    try {
      Call<List<Notification>> call = service.getNotifications(username);
      Response<List<Notification>> response = call.execute();

      if (response.code() == HttpURLConnection.HTTP_OK) {
        List<Notification> notifications = response.body();
        Log.e(TAG, response.body().toString());

        Realm realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        realm.where(Notification.class).findAll().clear();
        realm.copyToRealmOrUpdate(notifications);
        realm.commitTransaction();
        realm.close();

        notifySuccess(ACTION, "Successful");
      } else {
        String error = response.errorBody().string();
        Log.e(TAG, "Error: " + error);
        notifyFailure(ACTION, "Error");
      }

    } catch (IOException e) {
      Log.e(TAG, e.getMessage(), e);
      notifyFailure(ACTION, "Unable to connect to server");
    }
  }
  public void loadData() {
    SimpleDateFormat formatIn = new SimpleDateFormat("MMMM d, yyyy");
    SimpleDateFormat formatOut = new SimpleDateFormat("MM/d/yy");
    ObjectMapper objectMapper = new ObjectMapper();
    JsonFactory jsonFactory = new JsonFactory();
    Random random = new Random();
    try {
      JsonParser jsonParserBlog =
          jsonFactory.createParser(getResources().openRawResource(R.raw.blog));
      List<Blog> entries =
          objectMapper.readValue(jsonParserBlog, new TypeReference<List<Blog>>() {});
      JsonParser jsonParserEmoji =
          jsonFactory.createParser(getResources().openRawResource(R.raw.emoji));
      List<String> emojies =
          objectMapper.readValue(jsonParserEmoji, new TypeReference<List<String>>() {});

      int numEmoji = emojies.size();
      for (Blog blog : entries) {
        blog.setEmoji(emojies.get(random.nextInt(numEmoji)));
        try {
          blog.setDate(formatOut.format(formatIn.parse(blog.getDate())));
        } catch (ParseException e) {
          e.printStackTrace();
        }
      }
      Realm realm = Realm.getInstance(this);
      realm.beginTransaction();
      realm.copyToRealm(entries);
      realm.commitTransaction();
      realm.close();

    } catch (Exception e) {
      throw new IllegalStateException("Could not load blog data.");
    }
  }
Exemplo n.º 3
0
  @Override
  protected void onHandleIntent(Intent intent) {
    Log.e(TAG, "gettting donation requests");

    TbsService service = ServiceManager.getInstance();
    try {
      Call<List<DonateApproval>> call = service.getDonationRequests();
      Response<List<DonateApproval>> response = call.execute();

      if (response.code() == HttpURLConnection.HTTP_OK) {
        List<DonateApproval> donateApprovals = response.body();

        Realm realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        realm.where(DonateApproval.class).findAll().clear();
        realm.copyToRealmOrUpdate(donateApprovals);
        realm.commitTransaction();
        realm.close();

        notifySuccess(ACTION, "Successful");
      } else {
        String error = response.errorBody().string();
        Log.e(TAG, "Error: " + error);
        notifyFailure(ACTION, "Failed");
      }

    } catch (Exception e) {
      Log.e(TAG, e.getMessage(), e);
      notifyFailure(ACTION, "Request error");
    }
  }
Exemplo n.º 4
0
 public static void clearAllDatabaseCache() {
   // holder.timeOutMap.clear();
   Realm realm = Realm.getInstance(sContext);
   realm.clear(CacheInfo.class);
   realm.close();
   Log.d("aop-cache", "database cache has been cleared ");
 }
 public UiTag getTag(int tagId) {
   Realm realm = realmProvider.provideRealm();
   Tag tag = realm.where(Tag.class).equalTo("id", tagId).findFirst();
   UiTag uiTag = new UiTag(tag);
   realm.close();
   return uiTag;
 }
Exemplo n.º 6
0
  @Override
  public void tearDown() throws Exception {
    super.tearDown();

    realm.close();
    Realm.deleteRealmFile(getContext());
  }
  public void sum() {
    Log.i("DataStoreBenchmark", "Realm invoking sum");
    // super.intitalize();         Realm.deleteRealm(realmConfiguration);

    Realm realm;
    List<Long> values;
    long timeStart;
    values = new ArrayList<>();

    realm = Realm.getInstance(realmConfiguration);
    add(realm);
    check(realm);

    for (int i = 0; i < numberOfIterations; i++) {
      timeStart = Debug.threadCpuTimeNanos();
      System.gc();

      long sum = realm.where(Student.class).sumInt("points");
      long timeStop = Debug.threadCpuTimeNanos();
      long duration = timeStop - timeStart;
      values.add(duration);
    }

    realm.beginTransaction();
    realm.clear(Student.class);
    realm.commitTransaction();
    realm.close();

    finalvalues.put(SUM, values);

    System.gc();
  }
 @Override
 public void onDestroy() {
   super.onDestroy();
   if (realm != null) {
     realm.close();
     realm = null;
   }
 }
Exemplo n.º 9
0
  @Override
  protected void onDestroy() {
    super.onDestroy();

    if (realm != null) {
      realm.close();
    }
  }
Exemplo n.º 10
0
 public static void removeDataBaseCache(String classMethodString) {
   Realm realm = Realm.getInstance(sContext);
   CacheInfo info = realm.where(CacheInfo.class).equalTo("key", classMethodString).findFirst();
   if (info != null) {
     info.removeFromRealm();
   }
   realm.close();
 }
Exemplo n.º 11
0
 /** Close realm for current thread. */
 public static void close() throws IllegalStateException {
   Realm realm = realmCache.get();
   if (realm == null) {
     throw new IllegalStateException("realm already closed");
   }
   realm.close();
   realmCache.set(null);
 }
Exemplo n.º 12
0
  private static Integer getPhotoId() {
    Realm realm = RealmUtils.getRealm();
    RealmResults<Photo> list = realm.where(Photo.class).findAll().sort("id", Sort.ASCENDING);
    realm.close();

    if (list.size() == 0) {
      return 1;
    }
    return list.last().getId() + 1;
  }
  @Override
  public void call(final Subscriber<? super T> subscriber) {
    final Realm realm = Realm.getInstance(context);
    thread = Thread.currentThread();
    subscriber.add(
        Subscriptions.create(
            new Action0() {
              @Override
              public void call() {
                if (thread != null && !thread.isInterrupted()) {
                  thread.interrupt();
                }
              }
            }));

    boolean interrupted = false;
    boolean withError = false;

    T object = null;
    try {
      realm.beginTransaction();
      object = get(realm);
      interrupted = thread.isInterrupted();
      if (object != null && !interrupted) {
        realm.commitTransaction();
      } else {
        realm.cancelTransaction();
      }
    } catch (RuntimeException e) {
      realm.cancelTransaction();
      subscriber.onError(new RealmException("Error during transaction.", e));
      withError = true;
    } catch (Error e) {
      realm.cancelTransaction();
      subscriber.onError(e);
      withError = true;
    }
    if (!interrupted && !withError) {
      subscriber.onNext(object);
    }

    try {
      realm.close();
    } catch (RealmException ex) {
      subscriber.onError(ex);
      withError = true;
    }
    thread = null;
    if (!withError) {
      subscriber.onCompleted();
    }
    if (interrupted) {
      Thread.currentThread().interrupt();
    }
  }
Exemplo n.º 14
0
 public boolean checkIfTaskIsAdded(String taskName, String taskDescription) {
   Realm realm = Realm.getDefaultInstance();
   RealmResults<Task> getTasks =
       realm
           .where(Task.class)
           .equalTo("name", taskName)
           .equalTo("description", taskDescription)
           .findAll();
   boolean isAdded = getTasks.size() > 0;
   realm.close();
   return isAdded;
 }
Exemplo n.º 15
0
  private void initRealm() {
    RealmConfiguration realmConfiguration =
        new RealmConfiguration.Builder(getApplicationContext())
            .name(getString(R.string.db_name))
            .schemaVersion(getResources().getInteger(R.integer.db_version))
            .build();
    Realm.setDefaultConfiguration(realmConfiguration);

    Realm realm = Realm.getDefaultInstance();
    GroupRepository.createDefaultGroups(realm);
    realm.close();
  }
Exemplo n.º 16
0
 @Override
 public void onDestroy() {
   super.onDestroy();
   // Close adapter.
   if (adapter != null) adapter.close();
   // Close Realm.
   if (realm != null) {
     realm.close();
     realm = null;
   }
   if (actionMode != null) actionMode.finish();
 }
  @Override
  protected void tearDown() throws Exception {
    if (realm != null) {
      String path = realm.getPath();

      realm.close();

      if (!inMemory) {
        //noinspection ResultOfMethodCallIgnored
        new File(path).delete();
      }
    }

    super.tearDown();
  }
Exemplo n.º 18
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    copyBundledRealmFile(this.getResources().openRawResource(R.raw.default0), "default0");
    RealmConfiguration config0 =
        new RealmConfiguration.Builder(this)
            .name("default0")
            .schemaVersion(2)
            .migration(new Migration())
            .build();

    Realm.migrateRealm(config0, new Migration());
    realm = Realm.getInstance(config0);
    loadRealmData();
    realm.close();
  }
Exemplo n.º 19
0
  public void updateFinished(OpenweathermapObject data) {
    String errorMessage = getString(R.string.txt_unknown_error);

    if (data != null) {
      errorMessage = data.getErrorMessage();
    }

    if (errorMessage.isEmpty()) {
      SharedPreferences preferences =
          PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
      Boolean del_old =
          preferences.getBoolean(
              getString(R.string.settings_delete_old_data_key),
              getResources().getBoolean(R.bool.settings_delete_old_data_default));
      Realm realm = null;
      try {
        realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        if (del_old) {
          Long timeStamp = data.getWeatherForecastList().get(0).getTimeStamp();
          RealmResults<ForecastListItem> result =
              realm.where(ForecastListItem.class).lessThan("timeStamp", timeStamp).findAll();
          result.clear();
        }
        realm.copyToRealmOrUpdate(data.getWeatherForecastList());
        realm.commitTransaction();

        SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(getString(R.string.last_update_key), System.currentTimeMillis());
        String city = data.getCityName().concat(", ".concat(data.getCountry()));
        editor.putString(getString(R.string.current_city_key), city);
        editor.commit();

      } finally {
        if (realm != null) {
          realm.close();
        }
      }
    }
    Intent intent = new Intent(BROADCAST_ACTION);
    intent.putExtra(PARAM_STATUS, STATUS_FINISH);
    intent.putExtra(PARAM_RESULT, errorMessage);
    sendBroadcast(intent);
  }
  public void fullScan() {
    Log.i("DataStoreBenchmark", "Realm invoking full scan");
    // super.intitalize();
    Realm.deleteRealm(realmConfiguration);

    Realm realm;
    List<Long> values;
    long timeStart;
    values = new ArrayList<>();

    realm = Realm.getInstance(realmConfiguration);
    add(realm);
    check(realm);

    for (int i = 0; i < numberOfIterations; i++) {
      System.gc();
      timeStart = Debug.threadCpuTimeNanos();
      RealmResults<Student> realmResults =
          realm
              .where(Student.class)
              .equalTo("csci", true)
              .between("points", 110, 120)
              .equalTo("name", "S1")
              .findAll();
      long count = realmResults.size();
      long timeStop = Debug.threadCpuTimeNanos();
      long duration = timeStop - timeStart;
      values.add(duration);
    }
    realm.beginTransaction();
    realm.clear(Student.class);
    realm.commitTransaction();
    realm.close();

    finalvalues.put(FULL_SCAN, values);

    Realm.deleteRealm(realmConfiguration);
    super.garbaceColl();
  }
  public void batchWrite() {
    Log.i("DataStoreBenchmark", "Realm invoking batch write");
    // super.intitalize();
    Realm.deleteRealm(realmConfiguration);

    Realm realm;

    List<Long> values;
    long timeStart;
    values = new ArrayList<>();

    realm = Realm.getInstance(realmConfiguration);

    for (int j = 0; j < numberOfIterations; j++) {
      System.gc();
      timeStart = Debug.threadCpuTimeNanos();
      realm.beginTransaction();
      for (int i = 0; i < numberOfObjects; i++) {
        Student student = realm.createObject(Student.class);
        student.setStudentid(i);
        student.setName(dataGenerator.getStudentName(i));
        student.setCsci(dataGenerator.getCsciBool(i));
        student.setPoints(dataGenerator.getStudentPoints(i));
      }
      realm.commitTransaction();
      long timeStop = Debug.threadCpuTimeNanos();
      long duration = timeStop - timeStart;
      values.add(duration);
    }

    realm.beginTransaction();
    realm.clear(Student.class);
    realm.commitTransaction();
    realm.close();

    finalvalues.put(BATCH_WRITE, values);

    System.gc();
  }
  public void write() {
    Log.i("DataStoreBenchmark", "Realm invoking write");
    // super.intitalize();
    Realm.deleteRealm(realmConfiguration);

    Realm realm;
    int i;
    List<Long> values;
    long timeStart;
    values = new ArrayList<>();

    realm = Realm.getInstance(realmConfiguration);
    i = 0;

    for (int j = 0; j < numberOfIterations; j++) {
      System.gc();
      timeStart = Debug.threadCpuTimeNanos();
      realm.beginTransaction();
      Student student = realm.createObject(Student.class);
      student.setStudentid(999);
      student.setName("Student999");
      student.setCsci(true);
      student.setPoints(100);
      realm.commitTransaction();
      i++;
      long timeStop = Debug.threadCpuTimeNanos();
      long duration = timeStop - timeStart;
      values.add(duration);
    }

    realm.beginTransaction();
    realm.clear(Student.class);
    realm.commitTransaction();
    realm.close();

    finalvalues.put(SIMPLE_WRITE, values);
    System.gc();
  }
  public void simpleQuery() {
    // super.intitalize();
    Realm.deleteRealm(realmConfiguration);
    Log.i("DataStoreBenchmark", "Realm invoking simple query");

    Realm realm;
    List<Long> values;
    long timeStart;
    values = new ArrayList<>();

    realm = Realm.getInstance(realmConfiguration);
    add(realm);
    check(realm);

    for (int i = 0; i < numberOfIterations; i++) {
      System.gc();
      timeStart = Debug.threadCpuTimeNanos();
      RealmResults<Student> students =
          realm.where(Student.class).equalTo("csci", false).between("points", 80, 100).findAll();
      for (Student student : students) {
        long tmp = student.getStudentid();
      }
      long timeStop = Debug.threadCpuTimeNanos();
      long duration = timeStop - timeStart;
      values.add(duration);
    }

    realm.beginTransaction();
    realm.clear(Student.class);
    realm.commitTransaction();
    realm.close();

    finalvalues.put(SIMPLE_QUERY, values);

    System.gc();
  }
Exemplo n.º 24
0
 @Override
 protected Void doInTask(String... params) {
   String priceString = params[0];
   String myCode = LxbgApp.getInstance().LoginCode;
   fetchingJson(
       com.hlk.lxbg.customer.api.Order.OrderApi(),
       com.hlk.lxbg.customer.api.Order.UpPrice(myCode, orderId, priceString, orderUser));
   if (succeed && null != result && result.getS() == 0) {
     // 更改价格成功
     Realm realm = RealmConfig.getInstance().getRealm();
     try {
       Order order = new OrderImpl(realm).selectById(orderId);
       // 更新我对这个订单的报价状态
       realm.beginTransaction();
       order.setPrice(priceString);
       order.setPayState("1");
       realm.copyToRealmOrUpdate(order);
       realm.commitTransaction();
     } finally {
       realm.close();
     }
   }
   return null;
 }
Exemplo n.º 25
0
 @Override
 protected void onDestroy() {
   realm.close();
   super.onDestroy();
 }
 @Override
 public void onDestroyView() {
   super.onDestroyView();
   realm.close();
 }
Exemplo n.º 27
0
 @Override
 public void onStop() {
   super.onStop();
   mRealm.close();
 }
  @Override
  public void onDestroy() {
    super.onDestroy();

    mRealm.close();
  }
 @Override
 protected void onDestroy() {
   super.onDestroy();
   tagPopupMenuHelper.close();
   realm.close();
 }