private void deleteRun() { Log.i(TAG, "In Delete Run Menu, Runs in the adapter: " + mViewPager.getAdapter().getCount()); // First, stop location updates if the Run we're deleting is currently being tracked if (mRunManager.isTrackingRun(mRunManager.getRun(mRunId))) { try { mLocationService.send(Message.obtain(null, Constants.MESSAGE_STOP_LOCATION_UPDATES)); } catch (RemoteException e) { Log.i(TAG, "Caught RemoteException while trying to send MESSAGE_STOP_LOCATION_UPDATES"); } mRunManager.stopRun(); } Log.i(TAG, "Runs in Adapter before Run deletion: " + mAdapter.getCount()); // Now order the Run to be deleted. The Adapter, Subtitle and Loader will get reset // when the results of the Run deletion get reported to the ResultsReceiver Log.i(TAG, "Trying to delete Run " + mRunId); int locations = mRunManager.queryLocationsForRun(mRunId).getCount(); Log.i(TAG, "There are " + locations + " locations to be deleted for Run " + mRunId); mAdapter.startUpdate(mViewPager); TrackingLocationIntentService.startActionDeleteRun(this, mRunId); }
@Override public boolean onOptionsItemSelected(MenuItem item) { Log.i(TAG, "In onOptionsItemSelected(), mRunId is " + mRunId); Bundle args; // Create a new blank Run or change the sort order of the recorded Runs and the Activity's // subtitle to match switch (item.getItemId()) { case R.id.run_map_pager_activity_units: // Swap the measurement system value stored in shared prefs mRunManager .mPrefs .edit() .putBoolean( Constants.MEASUREMENT_SYSTEM, !mRunManager.mPrefs.getBoolean(Constants.MEASUREMENT_SYSTEM, Constants.IMPERIAL)) .apply(); Intent refreshIntent = new Intent(Constants.ACTION_REFRESH_MAPS); // Tell all the RunMapFragments to update so their textviews will display the newly-chosen // distance units boolean receiver = LocalBroadcastManager.getInstance(this).sendBroadcast(refreshIntent); if (!receiver) { Log.i(TAG, "No receiver for RunFragment REFRESH broadcast!"); } // We want to change the menuItem title in onPrepareOptionsMenu, so we need to invalidate // the menu and recreated it. invalidateOptionsMenu(); return true; case R.id.run_map_pager_activity_scroll: // This setting gets made in a RunMapFragment's MapView, but we want to put this together // with the measurement units menu item here in the Activity's OptionsMenu, so we first // have to retrieve the RunMapFragment that's currently displayed RunMapFragment fragment = (RunMapFragment) mAdapter.getRegisteredFragment(mViewPager.getCurrentItem()); fragment .mGoogleMap .getUiSettings() .setScrollGesturesEnabled( !fragment.mGoogleMap.getUiSettings().isScrollGesturesEnabled()); // We want to change the menuItem title in onPrepareOptionsMenu, so we need to invalidate // the menu and recreated it. invalidateOptionsMenu(); return true; case R.id.run_map_pager_menu_item_new_run: // Now that we're using an auto-updating loader for the list of runs, we don't need // to call startActivityForResult() - the loader and content observer system take // care of updating the list to reflect the presence of a new Run. Note that we // don't have to call restartLoader() on the RUN_LIST_LOADER because the query on // the database hasn't changed. TrackingLocationIntentService.startActionInsertRun(this, new Run()); return true; case R.id.menu_item_map_pager_delete_run: // Bring up a dialog asking for confirmation of deletion of this Run, passing along // identity of this Activity and that there's only one Run to be deleted so that // the dialog's message will be correct. Bundle bundle = new Bundle(); bundle.putInt(Constants.FRAGMENT, Constants.RUN_MAP_FRAGMENT); bundle.putInt(Constants.NUMBER_OF_RUNS, 1); DeleteRunsDialog dialog = new DeleteRunsDialog(); dialog.setArguments(bundle); dialog.show(getSupportFragmentManager(), "DeleteDialog"); return true; // To change the sort order, set mSortOrder, store it to SharedPrefs, reinitialize the // adapter and subtitle and restart the RunListLoader case R.id.run_map_pager_menu_item_sort_by_date_asc: mSortOrder = Constants.SORT_BY_DATE_ASC; break; case R.id.run_map_pager_menu_item_sort_by_date_desc: mSortOrder = Constants.SORT_BY_DATE_DESC; break; case R.id.run_map_pager_menu_item_sort_by_distance_asc: mSortOrder = Constants.SORT_BY_DISTANCE_ASC; break; case R.id.run_map_pager_menu_item_sort_by_distance_desc: mSortOrder = Constants.SORT_BY_DISTANCE_DESC; break; case R.id.run_map_pager_menu_item_sort_by_duration_asc: mSortOrder = Constants.SORT_BY_DURATION_ASC; break; case R.id.run_map_pager_menu_item_sort_by_duration_desc: mSortOrder = Constants.SORT_BY_DURATION_DESC; break; default: return super.onOptionsItemSelected(item); } // Save Sort Order to Shared Prefs so that when when we go back to the RunPagerActivity, // the Runs will be sorted in the same order mRunManager.mPrefs.edit().putInt(Constants.SORT_ORDER, mSortOrder).apply(); args = setupAdapterAndLoader(); getSupportLoaderManager().restartLoader(Constants.RUN_LIST_LOADER, args, this); return true; }