Skip to content

Commit

Permalink
Add an "Ask me everytime" option to the link type options (pR0Ps#29)
Browse files Browse the repository at this point in the history
* Default the setting to "Ask me every time"
* Re-implement the link type chooser dialogs
* When sharing, if "Ask me every time" is set, provide a link type chooser

Closes pR0Ps#22
  • Loading branch information
mahula authored and pR0Ps committed Aug 19, 2018
1 parent d6e8dc1 commit 23dd6b5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 23 deletions.
75 changes: 59 additions & 16 deletions app/src/main/java/ca/cmetcalfe/locationshare/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ca.cmetcalfe.locationshare;

import android.Manifest;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -155,6 +157,23 @@ private void updateLocation(Location location) {
}
}

// ----------------------------------------------------
// DialogInterface Listeners
// ----------------------------------------------------
private class onClickShareListener implements DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog, int i) {
shareLocationText(formatLocation(lastLocation, getResources().getStringArray(R.array.link_templates)[i]));
}
}

private class onClickCopyListener implements DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog, int i) {
copyLocationText(formatLocation(lastLocation, getResources().getStringArray(R.array.link_templates)[i]));
}
}

//-----------------------------------------------------
// Menu related methods
//-----------------------------------------------------
Expand Down Expand Up @@ -186,31 +205,34 @@ public void shareLocation(View view) {
return;
}

String link = formatLocation(lastLocation, PreferenceManager.getDefaultSharedPreferences(this).getString("prefLinkType", ""));
String linkChoice = PreferenceManager.getDefaultSharedPreferences(this).getString("prefLinkType", "");

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, link);
intent.setType("text/plain");
startActivity(Intent.createChooser(intent, getString(R.string.share_location_via)));
if (linkChoice.equals(getResources().getString(R.string.always_ask))) {
new AlertDialog.Builder(this).setTitle(R.string.choose_link)
.setCancelable(true)
.setItems(R.array.link_names, new onClickShareListener())
.create()
.show();
} else {
shareLocationText(formatLocation(lastLocation, linkChoice));
}
}

public void copyLocation(View view) {
if (!validLocation(lastLocation)) {
return;
}

String link = formatLocation(lastLocation, PreferenceManager.getDefaultSharedPreferences(this).getString("prefLinkType", ""));
String linkChoice = PreferenceManager.getDefaultSharedPreferences(this).getString("prefLinkType", "");

ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboard != null){
ClipData clip = ClipData.newPlainText(getString(R.string.app_name), link);
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), R.string.copied, Toast.LENGTH_SHORT).show();
}
else {
Log.e(TAG, "Failed to get the clipboard service");
Toast.makeText(getApplicationContext(), R.string.clipboard_error, Toast.LENGTH_SHORT).show();
if (linkChoice.equals(getResources().getString(R.string.always_ask))) {
new AlertDialog.Builder(this).setTitle(R.string.choose_link)
.setCancelable(true)
.setItems(R.array.link_names, new onClickCopyListener())
.create()
.show();
} else {
copyLocationText(formatLocation(lastLocation, linkChoice));
}
}

Expand All @@ -234,6 +256,27 @@ public void openLocationSettings(View view) {
// ----------------------------------------------------
// Helper functions
// ----------------------------------------------------
public void shareLocationText(String string){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, string);
intent.setType("text/plain");
startActivity(Intent.createChooser(intent, getString(R.string.share_location_via)));
}

public void copyLocationText(String string){
ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboard != null){
ClipData clip = ClipData.newPlainText(getString(R.string.app_name), string);
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), R.string.copied, Toast.LENGTH_SHORT).show();
}
else {
Log.e(TAG, "Failed to get the clipboard service");
Toast.makeText(getApplicationContext(), R.string.clipboard_error, Toast.LENGTH_SHORT).show();
}
}

private void startRequestingLocation() {
if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
return;
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

<string name="choose_link">Linktyp auswählen</string>

<string name="always_ask">Jedesmal nachfragen</string>

<string name="settings">Einstellungen</string>
<string name="link_type">Linktyp</string>
</resources>
2 changes: 0 additions & 2 deletions app/src/main/res/values-nb/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@

<string name="settings">Innstillinger</string>
<string name="link_type">Lenketype</string>
<string name="pref_link_type_default" translatable="false">https://maps.google.com/?q={0},{1}</string>

</resources>
14 changes: 14 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="settings_options">
<item>@string/googlemaps</item>
<item>@string/openstreetmap</item>
<item>@string/geouri</item>
<item>@string/always_ask</item>
</string-array>

<string-array name="link_names">
<item>@string/googlemaps</item>
<item>@string/openstreetmap</item>
<item>@string/geouri</item>
</string-array>

<string-array name="options_templates" translatable="false">
<item>https://maps.google.com/?q={0},{1}</item>
<item>https://openstreetmap.org/?mlat={0}&amp;mlon={1}</item>
<item>geo:{0},{1}</item>
<item>@string/always_ask</item>
</string-array>

<string-array name="link_templates" translatable="false">
<item>https://maps.google.com/?q={0},{1}</item>
<item>https://openstreetmap.org/?mlat={0}&amp;mlon={1}</item>
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@

<string name="choose_link">Choose a link type</string>

<string name="always_ask">Ask me every time</string>
<string name="googlemaps" translatable="false">Google Maps</string>
<string name="openstreetmap" translatable="false">OpenStreetMap</string>
<string name="geouri" translatable="false">GeoURI</string>

<string name="settings">Settings</string>
<string name="link_type">Link type</string>
<string name="pref_link_type_default" translatable="false">https://maps.google.com/?q={0},{1}</string>

</resources>
6 changes: 3 additions & 3 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:key="prefLinkType"
android:title="@string/link_type"
android:dialogTitle="@string/choose_link"
android:entries="@array/link_names"
android:entryValues="@array/link_templates"
android:defaultValue="@string/pref_link_type_default" />
android:entries="@array/settings_options"
android:entryValues="@array/options_templates"
android:defaultValue="@string/always_ask" />
</PreferenceScreen>

0 comments on commit 23dd6b5

Please sign in to comment.