-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
214 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/de/storchp/opentracks/osmplugin/DirectoryChooserActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.