Esempio n. 1
0
 void startBuy() {
   ClockworkModBillingClient.getInstance().refreshMarketPurchases();
   final ProgressDialog dlg = new ProgressDialog(MainActivity.this);
   dlg.setMessage(getString(R.string.retrieving_status));
   dlg.show();
   ThreadingRunnable.background(
       new ThreadingRunnable() {
         @Override
         public void run() {
           try {
             final HttpGet get = new HttpGet(ServiceHelper.STATUS_URL);
             final JSONObject payload =
                 ServiceHelper.retryExecuteAsJSONObject(
                     MainActivity.this, mSettings.getString("account"), get);
             final long expiration = payload.getLong("subscription_expiration");
             foreground(
                 new Runnable() {
                   @Override
                   public void run() {
                     refreshAccount(expiration);
                     dlg.dismiss();
                     Intent i = new Intent(MainActivity.this, BuyActivity.class);
                     i.putExtra("payload", payload.toString());
                     startActivityForResult(i, 2323);
                   }
                 });
           } catch (Exception ex) {
             foreground(
                 new Runnable() {
                   @Override
                   public void run() {
                     dlg.dismiss();
                     Helper.showAlertDialog(MainActivity.this, R.string.status_error);
                   }
                 });
             ex.printStackTrace();
           }
         }
       });
 }
Esempio n. 2
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String account = mSettings.getString("account");
    String registrationId = mSettings.getString("registration_id");

    if (mSettings.getLong("last_missed_call", 0) == 0) {
      mSettings.setLong("last_missed_call", System.currentTimeMillis());
    }

    mAccountItem =
        addItem(
            R.string.account,
            new ListItem(this, account, null) {
              @Override
              public void onClick(View view) {
                super.onClick(view);

                String account = mSettings.getString("account");
                String registrationId = mSettings.getString("registration_id");

                if (Helper.isJavaScriptNullOrEmpty(account)
                    || Helper.isJavaScriptNullOrEmpty(registrationId)) {
                  doLogin();
                } else {
                  AlertDialog.Builder builder = new Builder(MainActivity.this);
                  builder.setTitle(R.string.account);
                  builder.setItems(
                      new String[] {
                        getString(R.string.buy_desksms), getString(R.string.switch_account)
                      },
                      new OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                          if (which == 0) {
                            startBuy();
                          } else if (which == 1) {
                            doLogin();
                          }
                        }
                      });
                  builder.create().show();
                }
              }
            });

    final Runnable updateSettings =
        new Runnable() {
          @Override
          public void run() {
            final ListItem gmail = findItem(R.string.gmail);
            final ListItem gtalk = findItem(R.string.google_talk);
            final ListItem web = findItem(R.string.web);
            final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
            dialog.setMessage(getString(R.string.updating_settings));
            dialog.show();
            dialog.setCancelable(false);
            ServiceHelper.updateSettings(
                MainActivity.this,
                gtalk.getIsChecked(),
                gmail.getIsChecked(),
                web.getIsChecked(),
                new Callback<Boolean>() {
                  @Override
                  public void onCallback(final Boolean result) {
                    runOnUiThread(
                        new Runnable() {
                          @Override
                          public void run() {
                            dialog.dismiss();
                            if (!result) {
                              Helper.showAlertDialog(
                                  MainActivity.this, R.string.updating_settings_error);
                              gmail.setIsChecked(mSettings.getBoolean("forward_email", true));
                              gtalk.setIsChecked(mSettings.getBoolean("forward_xmpp", true));
                              web.setIsChecked(mSettings.getBoolean("forward_web", true));
                            }
                          }
                        });
                  }
                });
          }
        };

    addItem(
        R.string.notifications,
        new ListItem(this, R.string.gmail, 0) {
          {
            CheckboxVisible = true;
            setIsChecked(mSettings.getBoolean("forward_email", true));
          }

          @Override
          public void onClick(View view) {
            super.onClick(view);
            updateSettings.run();
          }
        });

    addItem(
        R.string.notifications,
        new ListItem(this, R.string.google_talk, 0) {
          {
            CheckboxVisible = true;
            setIsChecked(mSettings.getBoolean("forward_xmpp", true));
          }

          @Override
          public void onClick(View view) {
            super.onClick(view);
            updateSettings.run();
          }
        });

    addItem(
        R.string.notifications,
        new ListItem(this, R.string.web, 0) {
          {
            CheckboxVisible = true;
            setIsChecked(mSettings.getBoolean("forward_web", true));
          }

          @Override
          public void onClick(View view) {
            super.onClick(view);
            if (getIsChecked()) {
              // reset the sync counter so it resends the sms history
              mSettings.setLong("last_sms_sync", 0);
              mSettings.setLong("last_mms_sync", 0);
              mSettings.setLong("last_calls_sync", 0);
              Helper.startSyncService(MainActivity.this);
            }
            updateSettings.run();
          }
        });

    addItem(
        R.string.contacts,
        new ListItem(this, R.string.manage_blacklist, R.string.blacklist_summary) {
          @Override
          public void onClick(View view) {
            super.onClick(view);
            MainActivity.this.startActivity(new Intent(MainActivity.this, BlackListActivity.class));
          }
        });

    /*
    addItem(R.string.contacts, new ListItem(this, R.string.add_desksms_contact_info, R.string.add_desksms_contact_info_summary) {
        @Override
        public void onClick(View view) {
            super.onClick(view);
            addDeskSmsContactInfo(false);
        }
    });
    */

    addItem(
        R.string.contacts,
        new ListItem(this, R.string.remove_desksms_contact_info, 0) {
          @Override
          public void onClick(View view) {
            super.onClick(view);
            removeDeskSmsContactInfo();
          }
        });

    addItem(
        R.string.troubleshooting,
        new ListItem(this, R.string.test_message, 0) {
          @Override
          public void onClick(View view) {
            super.onClick(view);

            TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            final String number = tm.getLine1Number();

            if (number == null || "".equals(number)) {
              Helper.showAlertDialog(MainActivity.this, R.string.number_retrieval_error);
              return;
            }

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle(R.string.test_message);
            builder.setMessage(R.string.test_message_info);
            builder.setNegativeButton(android.R.string.cancel, null);
            builder.setPositiveButton(
                android.R.string.ok,
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {

                    String message = getString(R.string.test_message_content);
                    SmsManager sm = SmsManager.getDefault();
                    sm.sendTextMessage(number, null, message, null, null);
                  }
                });
            builder.setCancelable(true);
            builder.create().show();
          }
        });

    addItem(
        R.string.troubleshooting,
        new ListItem(
            this,
            getString(R.string.adjust_sms_date),
            getAdjustmentString() + "\n" + getString(R.string.adjust_sms_date_summary)) {
          @Override
          public void onClick(View view) {
            super.onClick(view);

            ArrayList<String> adjusts = new ArrayList<String>();
            for (int adjust = -12; adjust < 13; adjust++) {
              adjusts.add(getAdjustmentString(adjust));
            }

            String[] options = new String[adjusts.size()];
            options = adjusts.toArray(options);

            AlertDialog.Builder builder = new Builder(MainActivity.this);
            builder.setTitle(R.string.adjust_sms_date);
            builder.setItems(
                options,
                new OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, final int which) {
                    final ProgressDialog dlg = new ProgressDialog(MainActivity.this);
                    dlg.setMessage(getString(R.string.updating_settings));
                    dlg.show();

                    ThreadingRunnable.background(
                        new ThreadingRunnable() {
                          @Override
                          public void run() {
                            try {
                              AndroidHttpClient client = Helper.getHttpClient(MainActivity.this);
                              try {
                                String account = mSettings.getString("account");
                                HttpDelete delete =
                                    new HttpDelete(String.format(ServiceHelper.SMS_URL, account));
                                ServiceHelper.retryExecute(
                                    MainActivity.this, account, client, delete);
                                foreground(
                                    new Runnable() {
                                      @Override
                                      public void run() {
                                        dlg.dismiss();
                                        int adjust = -12 + which;
                                        mSettings.setInt("adjust_sms_date", adjust);
                                        setSummary(
                                            getAdjustmentString()
                                                + "\n"
                                                + getString(R.string.adjust_sms_date_summary));
                                        mSettings.setLong("last_sms_sync", 0);
                                        Helper.startSyncService(MainActivity.this, "sms");
                                      }
                                    });
                              } finally {
                                client.close();
                              }
                            } catch (Exception ex) {
                              ex.printStackTrace();
                              foreground(
                                  new Runnable() {
                                    @Override
                                    public void run() {
                                      dlg.dismiss();
                                      Helper.showAlertDialog(
                                          MainActivity.this, R.string.updating_settings_error);
                                    }
                                  });
                            }
                          }
                        });
                  }
                });
            builder.create().show();
          }
        });

    Helper.startSyncService(this);

    Intent intent = getIntent();
    if (Helper.isJavaScriptNullOrEmpty(account) || Helper.isJavaScriptNullOrEmpty(registrationId)) {
      doLogin();
    } else if (intent != null && intent.getBooleanExtra("relogin", false)) {
      intent.removeExtra("relogin");
      doLogin();
    } else {
      refreshAccount();
      ClockworkModBillingClient.getInstance().refreshMarketPurchases();
    }

    ServiceHelper.getSettings(
        this,
        new Callback<JSONObject>() {
          @Override
          public void onCallback(JSONObject result) {
            final boolean forward_xmpp = result.optBoolean("forward_xmpp", true);
            final boolean forward_email = result.optBoolean("forward_email", true);
            runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    ListItem gmail = findItem(R.string.gmail);
                    ListItem gtalk = findItem(R.string.google_talk);
                    gmail.setIsChecked(forward_email);
                    gtalk.setIsChecked(forward_xmpp);
                  }
                });
          }
        });
  }