Skip to content

Commit

Permalink
WIP: move menu items to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
pstorch committed Sep 23, 2023
1 parent 9ecb8fe commit f84db8b
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 101 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ android {
targetSdkVersion 34
versionCode 39
versionName "2.23.0"
applicationId "de.storchp.opentracks.osmplugin"

testInstrumentationRunnerArguments runnerBuilder: 'de.mannodermaus.junit5.AndroidJUnit5Builder'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
2 changes: 2 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".DirectoryChooserActivity$MapDirectoryChooserActivity"/>
<activity android:name=".DirectoryChooserActivity$ThemeDirectoryChooserActivity"/>
<activity
android:name=".MapsActivity"
android:theme="@style/AppTheme.NoActionBar"
Expand Down
75 changes: 0 additions & 75 deletions src/main/java/de/storchp/opentracks/osmplugin/BaseActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package de.storchp.opentracks.osmplugin;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.UriPermission;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
Expand All @@ -17,7 +13,6 @@

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.MenuCompat;
Expand All @@ -42,12 +37,7 @@ public void onCreateOptionsMenu(Menu menu, boolean showInfo) {
getMenuInflater().inflate(R.menu.maps, menu);

MenuCompat.setGroupDividerEnabled(menu, true);

menu.findItem(R.id.map_info).setVisible(showInfo);

if (BuildConfig.offline) {
menu.findItem(R.id.download_map).setVisible(false);
}
}

ActivityResultLauncher<Intent> settingsActivityResultLauncher = registerForActivityResult(
Expand All @@ -68,16 +58,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
showCompassSmoothingDialog();
} else if (itemId == R.id.stroke_width) {
showStrokeWidthDialog();
} else if (itemId == R.id.map_selection) {
startActivity(new Intent(this, MapSelectionActivity.class));
} else if (itemId == R.id.theme_selection) {
startActivity(new Intent(this, ThemeSelectionActivity.class));
} else if (itemId == R.id.map_folder) {
openDirectory(mapDirectoryLauncher);
} else if (itemId == R.id.theme_folder) {
openDirectory(themeDirectoryLauncher);
} else if (itemId == R.id.download_map) {
startActivity(new Intent(this, DownloadMapSelectionActivity.class));
} else if (itemId == R.id.overdraw_factor) {
showOverdrawFactorDialog();
} else if (itemId == R.id.tilecache_capacity_factor) {
Expand Down Expand Up @@ -317,61 +297,6 @@ public void onStopTrackingTouch(SeekBar seekBar) {
});
}

protected final ActivityResultLauncher<Intent> mapDirectoryLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
changeMapDirectory(result.getData().getData(), result.getData());
}
});

protected final ActivityResultLauncher<Intent> themeDirectoryLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
changeThemeDirectory(result.getData().getData(), result.getData());
}
});

protected void openDirectory(ActivityResultLauncher<Intent> launcher) {
try {
launcher.launch(createOpenDocumentIntent());
} catch (ActivityNotFoundException exception) {
Toast.makeText(BaseActivity.this, R.string.no_file_manager_found, Toast.LENGTH_LONG).show();
}
}

@NonNull
private Intent createOpenDocumentIntent() {
var intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
return intent;
}

private void releaseOldPermissions() {
getContentResolver().getPersistedUriPermissions().stream()
.map(UriPermission::getUri)
.filter(uri -> !uri.equals(PreferencesUtils.getMapDirectoryUri())
&& !uri.equals(PreferencesUtils.getMapThemeDirectoryUri()))
.forEach(uri -> getContentResolver().releasePersistableUriPermission(uri, 0));
}

protected void changeThemeDirectory(Uri uri, Intent resultData) {
takePersistableUriPermission(uri, resultData);
PreferencesUtils.setMapThemeDirectoryUri(uri);
releaseOldPermissions();
}

protected void changeMapDirectory(Uri uri, Intent resultData) {
takePersistableUriPermission(uri, resultData);
PreferencesUtils.setMapDirectoryUri(uri);
releaseOldPermissions();
}

private void takePersistableUriPermission(Uri uri, Intent intent) {
int takeFlags = intent.getFlags();
takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(uri, takeFlags);
}

protected void keepScreenOn(boolean keepScreenOn) {
if (keepScreenOn) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package de.storchp.opentracks.osmplugin;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.UriPermission;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import de.storchp.opentracks.osmplugin.utils.PreferencesUtils;

public abstract class DirectoryChooserActivity extends AppCompatActivity {

protected final ActivityResultLauncher<Intent> directoryIntentLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
onActivityResultCustom(result.getData());
}
finish();
});

abstract void onActivityResultCustom(@NonNull Intent resultData);

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

var intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);

try {
directoryIntentLauncher.launch(intent);
} catch (final ActivityNotFoundException exception) {
Toast.makeText(this, R.string.no_file_manager_found, Toast.LENGTH_LONG).show();
}
}

protected void takePersistableUriPermission(Uri uri, Intent intent) {
int takeFlags = intent.getFlags();
takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(uri, takeFlags);
}

protected void releaseOldPermissions() {
getContentResolver().getPersistedUriPermissions().stream()
.map(UriPermission::getUri)
.filter(uri -> !uri.equals(PreferencesUtils.getMapDirectoryUri())
&& !uri.equals(PreferencesUtils.getMapThemeDirectoryUri()))
.forEach(uri -> getContentResolver().releasePersistableUriPermission(uri, 0));
}

public static class MapDirectoryChooserActivity extends DirectoryChooserActivity {

@Override
void onActivityResultCustom(@NonNull final Intent resultData) {
takePersistableUriPermission(resultData.getData(), resultData);
PreferencesUtils.setMapDirectoryUri(resultData.getData());
releaseOldPermissions();
}
}

public static class ThemeDirectoryChooserActivity extends DirectoryChooserActivity {

@Override
void onActivityResultCustom(@NonNull final Intent resultData) {
takePersistableUriPermission(resultData.getData(), resultData);
PreferencesUtils.setMapThemeDirectoryUri(resultData.getData());
releaseOldPermissions();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ private boolean isDownloadInProgress() {
public void startDownload() {
var directoryUri = downloadType.getDirectoryUri();
if (directoryUri == null) {
openDirectory(downloadType.getLauncher(DownloadActivity.this));
// TODO: openDirectory(downloadType.getLauncher(DownloadActivity.this));
return;
}

var directoryFile = FileUtil.getDocumentFileFromTreeUri(this, directoryUri);
if (directoryFile == null || !directoryFile.canWrite()) {
openDirectory(downloadType.getLauncher(DownloadActivity.this));
// TODO: openDirectory(downloadType.getLauncher(DownloadActivity.this));
return;
}

Expand Down Expand Up @@ -170,6 +170,7 @@ private void downloadEnded(boolean success, boolean canceled) {
}
}

/* TODO
@Override
protected void changeMapDirectory(Uri uri, Intent resultData) {
super.changeMapDirectory(uri, resultData);
Expand All @@ -182,6 +183,8 @@ protected void changeThemeDirectory(Uri uri, Intent resultData) {
startDownload();
}
*/

private static class DownloadTask extends Thread {
private final WeakReference<DownloadActivity> ref;
private final Uri downloadUri;
Expand Down Expand Up @@ -331,7 +334,7 @@ public Uri getDirectoryUri() {

@Override
public ActivityResultLauncher<Intent> getLauncher(BaseActivity activity) {
return activity.mapDirectoryLauncher;
return null; // TODO: activity.mapDirectoryLauncher;
}
},
MAP_ZIP(R.string.overwrite_map_question, R.string.download_success, R.string.download_failed, true) {
Expand All @@ -341,7 +344,7 @@ public Uri getDirectoryUri() {
}
@Override
public ActivityResultLauncher<Intent> getLauncher(BaseActivity activity) {
return activity.mapDirectoryLauncher;
return null; // TODO: activity.mapDirectoryLauncher;
}
},
THEME(R.string.overwrite_theme_question, R.string.download_theme_success, R.string.download_theme_failed, false) {
Expand All @@ -351,7 +354,7 @@ public Uri getDirectoryUri() {
}
@Override
public ActivityResultLauncher<Intent> getLauncher(BaseActivity activity) {
return activity.themeDirectoryLauncher;
return null; // TODO: activity.themeDirectoryLauncher;
}
};

Expand Down Expand Up @@ -385,6 +388,7 @@ public boolean isExtractMapFromZIP() {
return extractMapFromZIP;
}

// TODO: check if this is still needed after refactoring
public abstract ActivityResultLauncher<Intent> getLauncher(BaseActivity activity);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package de.storchp.opentracks.osmplugin;

import static java.util.stream.Collectors.joining;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.documentfile.provider.DocumentFile;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

import java.util.Objects;

import de.storchp.opentracks.osmplugin.databinding.ActivitySettingsBinding;
import de.storchp.opentracks.osmplugin.utils.FileUtil;
import de.storchp.opentracks.osmplugin.utils.PreferencesUtils;

public class SettingsActivity extends AppCompatActivity {

Expand All @@ -28,6 +36,72 @@ public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);

var onlineMapConsentPreference = findPreference(getString(R.string.APP_PREF_ONLINE_MAP_CONSENT));
if (onlineMapConsentPreference != null && BuildConfig.offline) {
onlineMapConsentPreference.setVisible(false);
}

var mapDownloadPreference = findPreference(getString(R.string.APP_PREF_MAP_DOWNLOAD));
if (mapDownloadPreference != null && BuildConfig.offline) {
mapDownloadPreference.setVisible(false);
}

setSummaries();
}

@Override
public void onResume() {
super.onResume();
setSummaries();
}

private void setSummaries() {
var mapsPreference = findPreference(getString(R.string.APP_PREF_MAP_SELECTION));
if (mapsPreference != null) {
mapsPreference.setSummaryProvider((Preference.SummaryProvider<Preference>) preference -> {
var mapUris = PreferencesUtils.getMapUris();
if (mapUris.isEmpty() && !BuildConfig.offline) {
return getString(R.string.online_osm_mapnick);
}
return mapUris.stream()
.map(uri -> FileUtil.getDocumentFileFromTreeUri(getContext(), uri))
.filter(Objects::nonNull)
.map(DocumentFile::getName)
.collect(joining(", "));
});
}

var mapDirectoryPreference = findPreference(getString(R.string.APP_PREF_MAP_DIRECTORY));
if (mapDirectoryPreference != null) {
mapDirectoryPreference.setSummaryProvider((Preference.SummaryProvider<Preference>) preference -> {
var uri = PreferencesUtils.getMapDirectoryUri();
return uri != null ? uri.getLastPathSegment() : null;
});
}

var themePreference = findPreference(getString(R.string.APP_PREF_MAP_THEME));
if (themePreference != null) {
themePreference.setSummaryProvider((Preference.SummaryProvider<Preference>) preference -> {
var themeUri = PreferencesUtils.getMapThemeUri();
if (themeUri == null) {
return getString(R.string.default_theme);
}
var documentFile = FileUtil.getDocumentFileFromTreeUri(getContext(), themeUri);
if (documentFile == null) {
return getString(R.string.default_theme);
}
return documentFile.getName();
});
}

var themeDirectoryPreference = findPreference(getString(R.string.APP_PREF_MAP_THEME_DIRECTORY));
if (themeDirectoryPreference != null) {
themeDirectoryPreference.setSummaryProvider((Preference.SummaryProvider<Preference>) preference -> {
var uri = PreferencesUtils.getMapThemeDirectoryUri();
return uri != null ? uri.getLastPathSegment() : null;
});
}
}

}
Expand Down
11 changes: 0 additions & 11 deletions src/main/res/menu/maps.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@
android:icon="@drawable/ic_baseline_share_24"
/>

<group android:id="@+id/map_grp">
<item android:id="@+id/map_selection" android:title="@string/map_selection"/>
<item android:id="@+id/map_folder" android:title="@string/map_folder"/>
<item android:id="@+id/download_map" android:title="@string/download_map"/>
</group>

<group android:id="@+id/theme_grp">
<item android:id="@+id/theme_selection" android:title="@string/theme_selection"/>
<item android:id="@+id/theme_folder" android:title="@string/theme_folder"/>
</group>

<item
android:id="@+id/configure_statistic"
android:title="@string/configure_statistic"/>
Expand Down
2 changes: 2 additions & 0 deletions src/main/res/values/non-translatable-strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<string name="APP_MAP_TILE_CACHE_CAPACITY_FACTOR" translatable="false">APP_MAP_TILE_CACHE_CAPACITY_FACTOR</string>
<string name="APP_PREF_STATISTIC_ELEMENTS" translatable="false">APP_PREF_STATISTIC_ELEMENTS</string>
<string name="APP_PREF_TRACK_COLOR_MODE" translatable="false">APP_PREF_TRACK_COLOR_MODE</string>
<string name="APP_PREF_MAP_SELECTION" translatable="false">APP_PREF_MAP_SELECTION</string>
<string name="APP_PREF_MAP_DOWNLOAD" translatable="false">APP_PREF_MAP_DOWNLOAD</string>
<string name="osm_mapnik" translatable="false">OSM: Mapnik</string>
<string name="offline_maps" translatable="false"><b>- <a href="http://download.mapsforge.org/">Mapsforge</a></b>
\n<b>- <a href="https://www.freizeitkarte-osm.de/android/en/">Freizeitkarte Android</a></b>
Expand Down
Loading

0 comments on commit f84db8b

Please sign in to comment.