use of org.lucasr.twowayview.TwoWayView in project iNaturalistAndroid by inaturalist.
the class ObservationEditor method onCreate.
/**
* LIFECYCLE CALLBACKS
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
String action = intent != null ? intent.getAction() : null;
String type = intent != null ? intent.getType() : null;
if ((savedInstanceState == null) && (intent != null) && (intent.getData() != null)) {
int uriMatch = ObservationProvider.URI_MATCHER.match(intent.getData());
if ((uriMatch == Observation.OBSERVATIONS_URI_CODE) || (uriMatch == ObservationPhoto.OBSERVATION_PHOTOS_URI_CODE)) {
// Show the confirmation screen
mIsConfirmation = true;
} else {
mIsConfirmation = false;
}
} else if ((intent != null) && (action != null) && (Intent.ACTION_SEND.equals(action))) {
// Single share photo with iNaturalist
mIsConfirmation = true;
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
mSharePhotos = new ArrayList<>();
mSharePhotos.add(imageUri);
} else if ((intent != null) && (action != null) && (Intent.ACTION_SEND_MULTIPLE.equals(action))) {
// Multiple share photo with iNaturalist
mSharePhotos = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
} else if (savedInstanceState != null) {
// Show the observation editor screen
mIsConfirmation = savedInstanceState.getBoolean("mIsConfirmation", false);
}
mCameraPhotos = new ArrayList<String>();
setContentView(R.layout.observation_confirmation);
if (mIsConfirmation) {
setTitle(R.string.details);
} else {
setTitle(R.string.edit_observation);
}
if (mApp == null) {
mApp = (INaturalistApp) getApplicationContext();
}
if (mHelper == null) {
mHelper = new ActivityHelper(this);
}
if (mSharePhotos != null) {
// Share photos(s) with iNaturalist
mUri = getContentResolver().insert(Observation.CONTENT_URI, null);
if (mUri == null) {
Log.e(TAG, "Failed to insert new observation into " + Observation.CONTENT_URI);
finish();
return;
}
setResult(RESULT_OK, (new Intent()).setAction(mUri.toString()));
getIntent().setAction(Intent.ACTION_INSERT);
mPhotosAdded = new ArrayList<String>();
mPhotosRemoved = new ArrayList<ObservationPhoto>();
} else if (savedInstanceState == null) {
// Do some setup based on the action being performed.
Uri uri = intent.getData();
if (uri == null) {
Log.e(TAG, "Null URI from intent.getData");
finish();
return;
}
switch(ObservationProvider.URI_MATCHER.match(uri)) {
case Observation.OBSERVATION_ID_URI_CODE:
getIntent().setAction(Intent.ACTION_EDIT);
mUri = uri;
break;
case Observation.OBSERVATIONS_URI_CODE:
mUri = getContentResolver().insert(uri, null);
if (mUri == null) {
Log.e(TAG, "Failed to insert new observation into " + uri);
finish();
return;
}
setResult(RESULT_OK, (new Intent()).setAction(mUri.toString()));
getIntent().setAction(Intent.ACTION_INSERT);
break;
case ObservationPhoto.OBSERVATION_PHOTOS_URI_CODE:
mFileUri = (Uri) intent.getExtras().get("photoUri");
if (mFileUri == null) {
Toast.makeText(getApplicationContext(), getString(R.string.photo_not_specified), Toast.LENGTH_SHORT).show();
finish();
return;
}
mFileUri = getPath(this, mFileUri);
mUri = getContentResolver().insert(Observation.CONTENT_URI, null);
if (mUri == null) {
Log.e(TAG, "Failed to insert new observation into " + uri);
finish();
return;
}
mCursor = managedQuery(mUri, Observation.PROJECTION, null, null, null);
mObservation = new Observation(mCursor);
updateImageOrientation(mFileUri);
createObservationPhotoForPhoto(mFileUri);
setResult(RESULT_OK, (new Intent()).setAction(mUri.toString()));
getIntent().setAction(Intent.ACTION_INSERT);
mFileUri = null;
break;
default:
Log.e(TAG, "Unknown action, exiting");
finish();
return;
}
mPhotosAdded = new ArrayList<String>();
mPhotosRemoved = new ArrayList<ObservationPhoto>();
} else {
String fileUri = savedInstanceState.getString("mFileUri");
if (fileUri != null) {
mFileUri = Uri.parse(fileUri);
}
String obsUri = savedInstanceState.getString("mUri");
if (obsUri != null) {
mUri = Uri.parse(obsUri);
} else {
mUri = intent.getData();
}
mObservation = (Observation) savedInstanceState.getSerializable("mObservation");
mProjects = (ArrayList<BetterJSONObject>) savedInstanceState.getSerializable("mProjects");
mProjectIds = savedInstanceState.getIntegerArrayList("mProjectIds");
mProjectFieldValues = (HashMap<Integer, ProjectFieldValue>) savedInstanceState.getSerializable("mProjectFieldValues");
mProjectFieldsUpdated = savedInstanceState.getBoolean("mProjectFieldsUpdated");
mPictureTaken = savedInstanceState.getBoolean("mPictureTaken", false);
mPreviousTaxonSearch = savedInstanceState.getString("mPreviousTaxonSearch");
mTaxonPicUrl = savedInstanceState.getString("mTaxonPicUrl");
mIsCaptive = savedInstanceState.getBoolean("mIsCaptive", false);
mFirstPositionPhotoId = savedInstanceState.getString("mFirstPositionPhotoId");
mGettingLocation = savedInstanceState.getBoolean("mGettingLocation");
mLocationManuallySet = savedInstanceState.getBoolean("mLocationManuallySet");
mReturnToObservationList = savedInstanceState.getBoolean("mReturnToObservationList");
mPhotosChanged = savedInstanceState.getBoolean("mPhotosChanged");
mPhotosAdded = savedInstanceState.getStringArrayList("mPhotosAdded");
mPhotosRemoved = (ArrayList<ObservationPhoto>) savedInstanceState.getSerializable("mPhotosRemoved");
mCameraPhotos = savedInstanceState.getStringArrayList("mCameraPhotos");
mChoseNewPhoto = savedInstanceState.getBoolean("mChoseNewPhoto");
}
findViewById(R.id.locationVisibility).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mHelper.selection(getString(R.string.location_visibility), getResources().getStringArray(R.array.geoprivacy_items), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mGeoprivacy.setSelection(which);
updateObservationVisibilityDescription();
int index = mGeoprivacy.getSelectedItemPosition();
String value;
switch(index) {
case 2:
value = AnalyticsClient.EVENT_VALUE_GEOPRIVACY_PRIVATE;
break;
case 1:
value = AnalyticsClient.EVENT_VALUE_GEOPRIVACY_OBSCURED;
break;
case 0:
default:
value = AnalyticsClient.EVENT_VALUE_GEOPRIVACY_OPEN;
}
try {
JSONObject eventParams = new JSONObject();
eventParams.put(AnalyticsClient.EVENT_PARAM_NEW_VALUE, value);
AnalyticsClient.getInstance().logEvent(AnalyticsClient.EVENT_NAME_OBS_GEOPRIVACY_CHANGED, eventParams);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
findViewById(R.id.is_captive_checkbox).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mIsCaptive = !mIsCaptive;
if (mIsCaptive) {
findViewById(R.id.is_captive_on_icon).setVisibility(View.VISIBLE);
findViewById(R.id.is_captive_off_icon).setVisibility(View.GONE);
} else {
findViewById(R.id.is_captive_on_icon).setVisibility(View.GONE);
findViewById(R.id.is_captive_off_icon).setVisibility(View.VISIBLE);
}
try {
JSONObject eventParams = new JSONObject();
eventParams.put(AnalyticsClient.EVENT_PARAM_NEW_VALUE, mIsCaptive ? AnalyticsClient.EVENT_PARAM_VALUE_YES : AnalyticsClient.EVENT_PARAM_VALUE_NO);
AnalyticsClient.getInstance().logEvent(AnalyticsClient.EVENT_NAME_OBS_CAPTIVE_CHANGED, eventParams);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
mGeoprivacy = (Spinner) findViewById(R.id.geoprivacy);
mSpeciesGuessTextView = (EditText) findViewById(R.id.speciesGuess);
mSpeciesGuessSub = (TextView) findViewById(R.id.speciesGuessSub);
mClearSpeciesGuess = (ImageView) findViewById(R.id.clear_species_guess);
mSpeciesGuessIcon = (ImageView) findViewById(R.id.species_guess_icon);
mDescriptionTextView = (TextView) findViewById(R.id.description);
mClearSpeciesGuess.setVisibility(View.VISIBLE);
mClearSpeciesGuess.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
clearSpeciesGuess();
}
});
mDescriptionTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String newDescription = charSequence.toString().trim();
String originalDescription = mObservation.description != null ? mObservation.description.trim() : "";
if (!newDescription.equals(originalDescription)) {
AnalyticsClient.getInstance().logEvent(AnalyticsClient.EVENT_NAME_OBS_NOTES_CHANGED);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
mSaveButton = (TextView) findViewById(R.id.save_observation);
mObservedOnButton = (TextView) findViewById(R.id.observed_on);
mObservedOnStringTextView = (TextView) findViewById(R.id.observed_on_string);
mTimeObservedAtButton = (TextView) findViewById(R.id.time_observed_at);
mGallery = (TwoWayView) findViewById(R.id.gallery);
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
mGallery.setItemMargin((int) px);
mLatitudeView = (TextView) findViewById(R.id.latitude);
mLongitudeView = (TextView) findViewById(R.id.longitude);
mAccuracyView = (TextView) findViewById(R.id.accuracy);
mLocationProgressView = (ProgressBar) findViewById(R.id.locationProgress);
mLocationRefreshButton = (View) findViewById(R.id.locationRefreshButton);
mTopActionBar = getSupportActionBar();
mDeleteButton = (ImageButton) findViewById(R.id.delete_observation);
mViewOnInat = (ImageButton) findViewById(R.id.view_on_inat);
mProjectSelector = (TextView) findViewById(R.id.select_projects);
mProjectCount = (TextView) findViewById(R.id.project_count);
mProjectFieldsTable = (TableLayout) findViewById(R.id.project_fields);
mLocationIcon = (ImageView) findViewById(R.id.location_icon);
mLocationGuess = (TextView) findViewById(R.id.location_guess);
mFindingCurrentLocation = (TextView) findViewById(R.id.finding_current_location);
mCloseSpeciesNameOnboarding = findViewById(R.id.onboarding_species_name_close);
mSpeciesNameOnboarding = (ViewGroup) findViewById(R.id.onboarding_species_name);
mSpeciesNameOnboarding = (ViewGroup) findViewById(R.id.onboarding_species_name);
final SharedPreferences prefs = getSharedPreferences("iNaturalistPreferences", MODE_PRIVATE);
mCloseSpeciesNameOnboarding.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mSpeciesNameOnboarding.setVisibility(View.GONE);
prefs.edit().putBoolean("onboarded_species_guess", true).commit();
}
});
// Decide if to show onboarding message
boolean hasOnboardedSpeciesGuess = prefs.getBoolean("onboarded_species_guess", false);
mSpeciesNameOnboarding.setVisibility(hasOnboardedSpeciesGuess ? View.GONE : View.VISIBLE);
mProjectSelector.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ObservationEditor.this, ProjectSelectorActivity.class);
intent.putExtra(INaturalistService.OBSERVATION_ID, (mObservation.id == null ? mObservation._id : mObservation.id));
intent.putExtra(ProjectSelectorActivity.IS_CONFIRMATION, true);
intent.putExtra(ProjectSelectorActivity.PROJECT_FIELDS, mProjectFieldValues);
intent.putIntegerArrayListExtra(INaturalistService.PROJECT_ID, mProjectIds);
startActivityForResult(intent, PROJECT_SELECTOR_REQUEST_CODE);
}
});
findViewById(R.id.coordinates).setVisibility(View.GONE);
if (mTaxonPicUrl != null) {
UrlImageViewHelper.setUrlDrawable(mSpeciesGuessIcon, mTaxonPicUrl, R.drawable.ic_species_guess_black_24dp, new UrlImageViewCallback() {
@Override
public void onLoaded(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
mSpeciesGuessIcon.setAlpha(1.0f);
}
}
@Override
public Bitmap onPreSetBitmap(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) {
return loadedBitmap;
}
});
}
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View view) {
mTaxonSearchStarted = true;
if ((mGallery.getAdapter().getCount() == 0) || (!mApp.getSuggestSpecies())) {
// No photos / suggest species setting is off - show the regular species search (by name)
Intent intent = new Intent(ObservationEditor.this, TaxonSearchActivity.class);
intent.putExtra(TaxonSearchActivity.SPECIES_GUESS, mSpeciesGuessTextView.getText().toString());
intent.putExtra(TaxonSearchActivity.SHOW_UNKNOWN, true);
intent.putExtra(TaxonSearchActivity.OBSERVATION_ID, mObservation.id);
intent.putExtra(TaxonSearchActivity.OBSERVATION_ID_INTERNAL, mObservation._id);
intent.putExtra(TaxonSearchActivity.OBSERVATION_JSON, mObservation.toJSONObject().toString());
startActivityForResult(intent, TAXON_SEARCH_REQUEST_CODE);
} else {
// At least one photo - show taxon suggestions screen
Intent intent = new Intent(ObservationEditor.this, TaxonSuggestionsActivity.class);
int pos = mImageCursor.getPosition();
mImageCursor.moveToFirst();
intent.putExtra(TaxonSuggestionsActivity.OBS_PHOTO_FILENAME, mImageCursor.getString(mImageCursor.getColumnIndex(ObservationPhoto.PHOTO_FILENAME)));
intent.putExtra(TaxonSuggestionsActivity.OBS_PHOTO_URL, mImageCursor.getString(mImageCursor.getColumnIndex(ObservationPhoto.PHOTO_URL)));
mImageCursor.move(pos);
intent.putExtra(TaxonSuggestionsActivity.LONGITUDE, mObservation.longitude);
intent.putExtra(TaxonSuggestionsActivity.LATITUDE, mObservation.latitude);
intent.putExtra(TaxonSuggestionsActivity.OBSERVED_ON, mObservation.observed_on);
intent.putExtra(TaxonSuggestionsActivity.OBSERVATION_ID, mObservation.id);
intent.putExtra(TaxonSuggestionsActivity.OBSERVATION_ID_INTERNAL, mObservation._id);
intent.putExtra(TaxonSuggestionsActivity.OBSERVATION, mObservation.toJSONObject().toString());
startActivityForResult(intent, TAXON_SEARCH_REQUEST_CODE);
}
}
};
findViewById(R.id.species_guess_container).setOnClickListener(listener);
mSpeciesGuessTextView.setOnClickListener(listener);
mTopActionBar.setHomeButtonEnabled(true);
mTopActionBar.setDisplayHomeAsUpEnabled(true);
View takePhoto;
mTopActionBar.setLogo(R.drawable.ic_arrow_back);
mTopActionBar.setTitle(getString(R.string.details));
takePhoto = findViewById(R.id.take_photo);
takePhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openImageIntent(ObservationEditor.this);
}
});
mSpeciesGuess = intent.getStringExtra(SPECIES_GUESS);
initObservation();
if ((intent != null) && (!mPictureTaken)) {
if (intent.getBooleanExtra(TAKE_PHOTO, false)) {
// Immediately take a photo
takePhoto();
} else if (intent.getBooleanExtra(CHOOSE_PHOTO, false)) {
// Immediately choose an existing photo
mChoseNewPhoto = true;
choosePhoto();
}
}
initUi();
if (intent != null) {
String taxonJson = (String) mApp.getServiceResult(TAXON);
if (taxonJson != null) {
BetterJSONObject taxon = new BetterJSONObject(taxonJson);
JSONObject idPhoto = taxon.getJSONObject("default_photo");
int rankLevel = taxon.getInt("rank_level") != null ? taxon.getInt("rank_level") : 0;
setTaxon(getTaxonName(taxon.getJSONObject()), TaxonUtils.getTaxonScientificName(taxon.getJSONObject()), rankLevel, false, taxon.getInt("id"), idPhoto != null ? idPhoto.optString("square_url") : null, taxon.getString("iconic_taxon_name"));
mApp.setServiceResult(TAXON, null);
} else if (mObservation.taxon_id != null) {
// Taxon info not loaded - download it now
mTaxonReceiver = new TaxonReceiver();
IntentFilter filter = new IntentFilter(INaturalistService.ACTION_GET_TAXON_NEW_RESULT);
Log.i(TAG, "Registering ACTION_GET_TAXON_NEW_RESULT");
BaseFragmentActivity.safeRegisterReceiver(mTaxonReceiver, filter, this);
Intent serviceIntent = new Intent(INaturalistService.ACTION_GET_TAXON_NEW, null, this, INaturalistService.class);
serviceIntent.putExtra(INaturalistService.TAXON_ID, mObservation.taxon_id);
startService(serviceIntent);
}
}
mObservedOnButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
mTimeObservedAtButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
mLocationRefreshButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(ObservationEditor.this);
// Set the adapter
String[] items = { getResources().getString(R.string.get_current_location), getResources().getString(R.string.edit_location) };
builder.setAdapter(new ArrayAdapter<String>(ObservationEditor.this, android.R.layout.simple_list_item_1, items), null);
final AlertDialog alertDialog = builder.create();
ListView listView = alertDialog.getListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
alertDialog.dismiss();
if (position == 0) {
// Get current place
mLocationManuallySet = true;
getLocation();
} else {
// Edit place
Intent intent = new Intent(ObservationEditor.this, LocationChooserActivity.class);
Double lat, lon;
lat = mObservation.private_latitude == null ? mObservation.latitude : mObservation.private_latitude;
lon = mObservation.private_longitude == null ? mObservation.longitude : mObservation.private_longitude;
intent.putExtra(LocationChooserActivity.LONGITUDE, lon);
intent.putExtra(LocationChooserActivity.LATITUDE, lat);
intent.putExtra(LocationChooserActivity.ACCURACY, (mObservation.positional_accuracy != null ? mObservation.positional_accuracy.doubleValue() : 0));
intent.putExtra(LocationChooserActivity.ICONIC_TAXON_NAME, mObservation.iconic_taxon_name);
startActivityForResult(intent, LOCATION_CHOOSER_REQUEST_CODE);
}
}
});
alertDialog.show();
}
});
if (getCurrentFocus() != null) {
// Hide keyboard
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
if (mProjectIds == null) {
if ((intent != null) && (intent.hasExtra(OBSERVATION_PROJECT))) {
Integer projectId = intent.getIntExtra(OBSERVATION_PROJECT, 0);
mProjectIds = new ArrayList<Integer>();
mProjectIds.add(projectId);
} else {
// Get IDs of project-observations
int obsId = (mObservation.id == null ? mObservation._id : mObservation.id);
Cursor c = getContentResolver().query(ProjectObservation.CONTENT_URI, ProjectObservation.PROJECTION, "(observation_id = " + obsId + ") AND ((is_deleted = 0) OR (is_deleted is NULL))", null, ProjectObservation.DEFAULT_SORT_ORDER);
c.moveToFirst();
mProjectIds = new ArrayList<Integer>();
while (c.isAfterLast() == false) {
ProjectObservation projectObservation = new ProjectObservation(c);
mProjectIds.add(projectObservation.project_id);
c.moveToNext();
}
c.close();
}
}
refreshProjectFields();
mProjectReceiver = new ProjectReceiver();
IntentFilter filter = new IntentFilter(INaturalistService.ACTION_JOINED_PROJECTS_RESULT);
BaseFragmentActivity.safeRegisterReceiver(mProjectReceiver, filter, this);
if (intent != null) {
mReturnToObservationList = intent.getBooleanExtra(RETURN_TO_OBSERVATION_LIST, false);
}
updateObservationVisibilityDescription();
((SwipeableLinearLayout) findViewById(R.id.swipeable_layout)).setOnSwipeListener(new SwipeableLinearLayout.SwipeListener() {
@Override
public void onSwipeRight() {
editNextObservation(-1);
}
@Override
public void onSwipeLeft() {
editNextObservation(1);
}
});
if (mSharePhotos != null) {
stopGetLocation();
// Share photos(s) with iNaturalist (override any place with the one from the shared images)
importPhotos(mSharePhotos, true);
}
}
Aggregations