Skip to content

Commit

Permalink
Fix some deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Dec 19, 2023
1 parent ae6e163 commit 460289b
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 103 deletions.
5 changes: 2 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.recyclerview:recyclerview:1.3.2'
implementation "androidx.preference:preference:1.2.1"
implementation "androidx.work:work-runtime:2.8.1"
implementation "androidx.work:work-runtime:2.9.0"
implementation 'androidx.core:core-splashscreen:1.0.1'
implementation 'com.google.code.findbugs:jsr305:3.0.2'
implementation 'androidx.annotation:annotation:1.7.0'
Expand All @@ -88,8 +88,7 @@ dependencies {
implementation 'com.github.andreynovikov:Geo-Coordinate-Conversion-Java:v1.0.0'
implementation 'com.caverock:androidsvg:1.4'
implementation 'org.slf4j:slf4j-api:2.0.9'
implementation 'org.slf4j:slf4j-jdk14:2.0.9'
implementation 'com.noveogroup.android:android-logger:1.3.6'
implementation 'com.github.tony19:logback-android:3.0.0'
implementation 'org.greenrobot:eventbus:3.0.0'
implementation 'com.squareup.okhttp3:okhttp:4.8.1'
implementation 'org.openstreetmap.osmosis:osmosis-osm-binary:0.48.3'
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/assets/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<configuration
xmlns="https://tony19.github.io/logback-android/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://tony19.github.io/logback-android/xml https://cdn.jsdelivr.net/gh/tony19/logback-android/logback.xsd"
>
<appender name="logcat" class="ch.qos.logback.classic.android.LogcatAppender">
<tagEncoder>
<pattern>%logger{32}</pattern>
</tagEncoder>
<encoder>
<pattern>[%-10thread] %msg</pattern>
</encoder>
</appender>

<logger name="org.oscim.theme.XmlThemeBuilder" level="INFO">
<appender-ref ref="logcat" />
</logger>
<logger name="org.oscim" level="WARN">
<appender-ref ref="logcat" />
</logger>

<logger name="mobi.maptrek.DataImportActivity" level="DEBUG">
<appender-ref ref="logcat" />
</logger>

<root level="INFO">
<appender-ref ref="logcat" />
</root>
</configuration>
77 changes: 43 additions & 34 deletions app/src/main/java/mobi/maptrek/DataImportActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
Expand All @@ -37,6 +35,10 @@
import android.provider.OpenableColumns;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
Expand All @@ -63,7 +65,7 @@
import mobi.maptrek.util.ProgressHandler;
import mobi.maptrek.util.ProgressListener;

public class DataImportActivity extends Activity {
public class DataImportActivity extends FragmentActivity {
private static final Logger logger = LoggerFactory.getLogger(DataImportActivity.class);
private static final String DATA_IMPORT_FRAGMENT = "dataImportFragment";
private static final DateFormat SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss", Locale.ROOT);
Expand All @@ -87,7 +89,7 @@ protected void onCreate(Bundle savedInstanceState) {
mProgressBar = findViewById(R.id.progressBar);
mActionButton = findViewById(R.id.action);

FragmentManager fm = getFragmentManager();
FragmentManager fm = getSupportFragmentManager();
mDataImportFragment = (DataImportFragment) fm.findFragmentByTag(DATA_IMPORT_FRAGMENT);
if (mDataImportFragment == null) {
mDataImportFragment = new DataImportFragment();
Expand Down Expand Up @@ -171,19 +173,15 @@ private void askForPermission(Runnable task) {

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (mTask != null)
mDataImportFragment.startImport(mTask);
} else {
finish();
}
// return;
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE) {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (mTask != null)
mDataImportFragment.startImport(mTask);
} else {
finish();
}
// other 'case' lines to check for other
// permissions this app might request
}
}

Expand Down Expand Up @@ -273,19 +271,21 @@ public void setProgressHandler(ProgressListener listener) {
private void processIntent(final Intent intent) {
String action = intent.getAction();
String type = intent.getType();
logger.debug("Action: {}", action);
logger.error("Action: {}", action);
logger.debug("Type: {}", type);
if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_VIEW.equals(action)) {
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (uri == null)
uri = intent.getData();
logger.debug("Uri: {}", uri.toString());
if (uri == null)
return;
logger.debug("Uri: {}", uri);
logger.debug("Authority: {}", uri.getAuthority());
final Uri finalUri = uri;
Runnable task = () -> readFile(finalUri);
String scheme = uri.getScheme();
if ("file".equals(scheme)) {
((DataImportActivity) getActivity()).askForPermission(task);
((DataImportActivity) requireActivity()).askForPermission(task);
} else {
startImport(task);
}
Expand All @@ -302,18 +302,20 @@ private void processIntent(final Intent intent) {
}

private void readFile(Uri uri) {
DataImportActivity activity = (DataImportActivity) getActivity();
DataImportActivity activity = (DataImportActivity) requireActivity();

String name = null;
long length = -1;

String scheme = uri.getScheme();
if ("file".equals(scheme)) {
String path = uri.getPath();
File src = new File(path);
name = uri.getLastPathSegment();
length = src.length();
try {
String path = uri.getPath();
if (path == null)
throw new FileNotFoundException();
File src = new File(path);
name = uri.getLastPathSegment();
length = src.length();
mInputStream = new MonitoredInputStream(new FileInputStream(src));
} catch (FileNotFoundException e) {
logger.error("Failed to get imported file stream", e);
Expand All @@ -326,9 +328,11 @@ private void readFile(Uri uri) {
Cursor cursor = resolver.query(uri, new String[]{OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE}, null, null, null);
if (cursor != null) {
logger.debug(" from cursor");
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
length = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
if (nameIndex >= 0 && sizeIndex >= 0 && cursor.moveToFirst()) {
name = cursor.getString(nameIndex);
length = cursor.getLong(sizeIndex);
}
cursor.close();
}
Expand All @@ -341,11 +345,10 @@ private void readFile(Uri uri) {
cursor.close();
}
if (length == -1) {
try {
AssetFileDescriptor afd = resolver.openAssetFileDescriptor(uri, "r");
try (AssetFileDescriptor afd = resolver.openAssetFileDescriptor(uri, "r")) {
if (afd != null)
length = afd.getLength();
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
Expand All @@ -370,6 +373,12 @@ private void readFile(Uri uri) {
return;
}

if (name == null) {
logger.error("Failed to get file name");
activity.showError(getString(R.string.msgFailedToGetFileName));
return;
}

if (!name.endsWith(TrackManager.EXTENSION) &&
!name.endsWith(KMLManager.EXTENSION) &&
!name.endsWith(KMLManager.ZIP_EXTENSION) &&
Expand All @@ -385,7 +394,7 @@ private void readFile(Uri uri) {
mProgressListener.onProgressAnnotated(name);
File dst = null;
try {
dst = getDestinationFile(name);
dst = getDestinationFile(activity, name);

mInputStream.addChangeListener(location -> {
if (mProgressListener != null) {
Expand All @@ -406,9 +415,9 @@ private void readFile(Uri uri) {
}

@Nullable
private File getDestinationFile(String filename) {
private File getDestinationFile(@NonNull Context context, String filename) {
boolean isMap = filename.endsWith(".mbtiles") || filename.endsWith(".sqlitedb");
File dir = getContext().getExternalFilesDir(isMap ? "maps" : "data");
File dir = context.getExternalFilesDir(isMap ? "maps" : "data");
if (dir == null) {
logger.error("Path for {} unavailable", isMap ? "maps" : "data");
return null;
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/mobi/maptrek/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

package mobi.maptrek;

import android.content.AsyncTaskLoader;
import android.content.Context;
import android.os.FileObserver;
import android.util.Pair;

import androidx.loader.content.AsyncTaskLoader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -216,7 +217,7 @@ protected void onStartLoading() {
logger.debug("onStartLoading()");
if (mData != null) {
// Deliver any previously loaded data immediately.
deliverResult(new ArrayList<FileDataSource>());
deliverResult(new ArrayList<>());
}

// Begin monitoring the underlying data source.
Expand Down
Loading

0 comments on commit 460289b

Please sign in to comment.