public void exportTasks( final Context context, final ExportType exportType, DialogBuilder dialogBuilder) { this.context = context; this.exportCount = 0; this.backupDirectory = preferences.getBackupDirectory(); this.latestSetVersionName = null; handler = exportType == ExportType.EXPORT_TYPE_MANUAL ? new Handler() : null; if (exportType == ExportType.EXPORT_TYPE_MANUAL) { progressDialog = dialogBuilder.newProgressDialog(); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setProgress(0); progressDialog.setCancelable(false); progressDialog.setIndeterminate(false); progressDialog.show(); if (context instanceof Activity) { progressDialog.setOwnerActivity((Activity) context); } } else { progressDialog = new ProgressDialog(context); } new Thread( new Runnable() { @Override public void run() { try { String output = setupFile(backupDirectory, exportType); int tasks = taskService.countTasks(); if (tasks > 0) { doTasksExport(output); } preferences.setLong(PREF_BACKUP_LAST_DATE, DateUtilities.now()); if (exportType == ExportType.EXPORT_TYPE_MANUAL) { onFinishExport(output); } } catch (IOException e) { Timber.e(e, e.getMessage()); } finally { post( new Runnable() { @Override public void run() { if (progressDialog.isShowing() && context instanceof Activity) { DialogUtilities.dismissDialog((Activity) context, progressDialog); ((Activity) context).finish(); } } }); } } }) .start(); }
public void getTrends() { new Thread( new Runnable() { @Override public void run() { try { Twitter twitter = Utils.getTwitter(context, settings); twitter4j.Trends trends = twitter.getPlaceTrends(1); final ArrayList<String> currentTrends = new ArrayList<String>(); for (Trend t : trends.getTrends()) { String name = t.getName(); currentTrends.add(name); } ((Activity) context) .runOnUiThread( new Runnable() { @Override public void run() { if (currentTrends != null) { listView.setAdapter(new TrendsArrayAdapter(context, currentTrends)); } listView.setVisibility(View.VISIBLE); LinearLayout spinner = (LinearLayout) layout.findViewById(R.id.list_progress); spinner.setVisibility(View.GONE); } }); HashtagDataSource source = HashtagDataSource.getInstance(context); for (String s : currentTrends) { if (s.contains("#")) { // we want to add it to the userAutoComplete Log.v("Test_hashtag", "adding: " + s); // could be much more efficient by querying and checking first, but I // just didn't feel like it when there is only ever 10 of them here source.deleteTag(s); // add it to the userAutoComplete database source.createTag(s); } } } catch (Throwable e) { e.printStackTrace(); } } }) .start(); }
/** Tweet image with text */ public static void tweet(final Context context, String message, File image) { /** Put the tweet message and image in the status object */ StatusUpdate statusUpdate = new StatusUpdate(message); statusUpdate.setMedia(image); LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); statusUpdate.setLocation(new GeoLocation(latitude, longitude)); } Log.d(TAG, "Attempting to tweet"); final StatusUpdate status = statusUpdate; /** If the user is logged in, we tweet! Once again, we use the Observable pattern. */ if (isLoggedIn) { new Thread() { @Override public void run() { try { Log.d(TAG, "Tweeting..."); Observable.just(twitter.updateStatus(status)) .subscribeOn(Schedulers.io()) .subscribe( new Action1<Status>() { @Override public void call(Status status) { Log.d(TAG, "Tweeted!"); if (context instanceof Activity) { ((Activity) context) .runOnUiThread( new Runnable() { public void run() { Toast.makeText( context, "Tweeted! Check your feed to see if it worked.", Toast.LENGTH_SHORT) .show(); } }); } } }); } catch (TwitterException e) { e.printStackTrace(); } } }.start(); } else { Log.d(TAG, "Oh no! We're not logged in"); Toast.makeText(context, "Log in to tweet", Toast.LENGTH_SHORT).show(); } }
/** * Perform upgrade from one version to the next. Needs to be called on the UI thread so it can * display a progress bar and then show users a change log. * * @param from * @param to */ public void performUpgrade(final Context context, final int from) { if (from == 135) AddOnService.recordOem(); // pop up a progress dialog final ProgressDialog dialog; if (context instanceof Activity) dialog = DialogUtilities.progressDialog(context, context.getString(R.string.DLG_upgrading)); else dialog = null; new Thread( new Runnable() { @Override public void run() { try { if (from < V3_0_0) new Astrid2To3UpgradeHelper().upgrade2To3(context, from); if (from < V3_1_0) new Astrid2To3UpgradeHelper().upgrade3To3_1(context, from); if (from < V3_3_0) upgrade3To3_3(context); } finally { if (context instanceof Activity) { ((Activity) context) .runOnUiThread( new Runnable() { public void run() { if (dialog != null) dialog.dismiss(); // display changelog showChangeLog(context, from); if (context instanceof TaskListActivity) ((TaskListActivity) context).loadTaskListContent(true); } }); } } } }) .start(); }
public void getPeople() { new Thread( new Runnable() { @Override public void run() { try { Twitter twitter = Utils.getTwitter(context, settings); ResponseList<User> usersResponse = twitter.getUserSuggestions(slug); users.clear(); for (User i : usersResponse) { users.add(i); } ((Activity) context) .runOnUiThread( new Runnable() { @Override public void run() { adapter = new PeopleArrayAdapter(context, users); listView.setAdapter(adapter); listView.setVisibility(View.VISIBLE); spinner.setVisibility(View.GONE); } }); } catch (Exception e) { e.printStackTrace(); } catch (OutOfMemoryError e) { e.printStackTrace(); } } }) .start(); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: sharedPrefs.edit().putBoolean("should_refresh", false).commit(); onBackPressed(); return true; case R.id.menu_settings: Intent settings = new Intent(context, SettingsActivity.class); startActivityForResult(settings, SETTINGS_RESULT); return true; case R.id.menu_save_search: Toast.makeText(context, getString(R.string.saving_search), Toast.LENGTH_SHORT).show(); new Thread( new Runnable() { @Override public void run() { try { Twitter twitter = Utils.getTwitter(context, AppSettings.getInstance(context)); twitter.createSavedSearch( searchQuery.replace(" -RT", "").replace(" TOP", "")); ((Activity) context) .runOnUiThread( new Runnable() { @Override public void run() { Toast.makeText( context, getString(R.string.success), Toast.LENGTH_SHORT) .show(); } }); } catch (TwitterException e) { // something went wrong } } }) .start(); return super.onOptionsItemSelected(item); case R.id.menu_compose_with_search: Intent compose = new Intent(context, ComposeActivity.class); compose.putExtra("user", searchQuery); startActivity(compose); return super.onOptionsItemSelected(item); case R.id.menu_search: // overridePendingTransition(0,0); // finish(); // overridePendingTransition(0,0); // return super.onOptionsItemSelected(item); case R.id.menu_pic_filter: if (!item.isChecked()) { searchQuery += " filter:links twitter.com"; item.setChecked(true); } else { searchQuery = searchQuery.replace("filter:links", "").replace("twitter.com", ""); item.setChecked(false); } Intent broadcast = new Intent("com.klinker.android.twitter.NEW_SEARCH"); broadcast.putExtra("query", searchQuery); context.sendBroadcast(broadcast); return super.onOptionsItemSelected(item); case R.id.menu_remove_rt: if (!item.isChecked()) { searchQuery += " -RT"; item.setChecked(true); } else { searchQuery = searchQuery.replace(" -RT", ""); item.setChecked(false); } broadcast = new Intent("com.klinker.android.twitter.NEW_SEARCH"); broadcast.putExtra("query", searchQuery); context.sendBroadcast(broadcast); return super.onOptionsItemSelected(item); case R.id.menu_show_top_tweets: if (!item.isChecked()) { searchQuery += " TOP"; item.setChecked(true); } else { searchQuery = searchQuery.replace(" TOP", ""); item.setChecked(false); } broadcast = new Intent("com.klinker.android.twitter.NEW_SEARCH"); broadcast.putExtra("query", searchQuery); context.sendBroadcast(broadcast); return super.onOptionsItemSelected(item); default: return super.onOptionsItemSelected(item); } }