Skip to content

Commit

Permalink
Updates for latest Android versions
Browse files Browse the repository at this point in the history
Signed-off-by: sunilpaulmathew <[email protected]>
  • Loading branch information
sunilpaulmathew committed Jun 5, 2021
1 parent 60142a8 commit 461bc1e
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 44 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ android {
}

compileSdkVersion 30
buildToolsVersion '29.0.3'
buildToolsVersion '30.0.3'

defaultConfig {
applicationId "com.sunilpaulmathew.translator"
minSdkVersion 23
targetSdkVersion 29
targetSdkVersion 30
versionCode 13
versionName "v0.13"
vectorDrawables.useSupportLibrary = true
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />

<application
android:allowBackup="true"
android:fullBackupContent="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:requestLegacyExternalStorage="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:requestLegacyExternalStorage="true"
android:supportsRtl="true"
tools:targetApi="q" >

<activity android:name="com.sunilpaulmathew.translator.MainActivity">
Expand All @@ -27,6 +28,7 @@
</activity>
<activity android:name=".activities.AboutActivity" />
<activity android:name=".activities.FindReplaceActivity" />
<activity android:name=".activities.InsertStringActivity" />
<activity android:name=".activities.SettingsActivity" />
<activity android:name=".activities.StringViewActivity" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class MainActivity extends AppCompatActivity {

public static AppCompatEditText mSearchWord;
private boolean mExit;
private Handler mHandler = new Handler();
private final Handler mHandler = new Handler();
private MaterialTextView mAboutApp;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2021-2022 sunilpaulmathew <[email protected]>
*
* This file is part of The Translator, An application to help translate android apps.
*
*/

package com.sunilpaulmathew.translator.activities;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textview.MaterialTextView;
import com.sunilpaulmathew.translator.R;
import com.sunilpaulmathew.translator.utils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/*
* Created by sunilpaulmathew <[email protected]> on June 05, 2021
*/
public class InsertStringActivity extends AppCompatActivity {

private AppCompatEditText mText;

@SuppressLint("SetTextI18n")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert_string);

mText = findViewById(R.id.text);
MaterialTextView mCancel = findViewById(R.id.cancel_button);
MaterialTextView mInsert = findViewById(R.id.insert_button);

mText.setTextColor(Utils.isDarkTheme(this) ? getResources().getColor(R.color.white) : getResources()
.getColor(R.color.black));

mInsert.setOnClickListener(v -> {
if (mText.getText() == null || mText.getText().toString().isEmpty()) {
return;
}
if (getStrings().size() > 0) {
Utils.create(getStrings().toString().replace("[","").replace("]","")
.replace(",","\n"), getFilesDir().toString() + "/strings.xml");
Utils.restartApp(this);
} else {
Utils.showSnackbar(findViewById(android.R.id.content), getString(R.string.insert_invalid_string));
}
});

mCancel.setOnClickListener(v -> onBackPressed());
}

private List<String> getStrings() {
List<String> mData = new ArrayList<>();
for (String line : Objects.requireNonNull(mText.getText()).toString().split("\\r?\\n")) {
if (line.contains("<string name=") && line.endsWith("</string>") && !line.contains("translatable=\"false")) {
mData.add(line);
}
}
return mData;
}

@Override
public void onBackPressed() {
if (mText.getText() != null && !mText.getText().toString().isEmpty()) {
new MaterialAlertDialogBuilder(this)
.setMessage(getString(R.string.discard_message))
.setNegativeButton(getString(R.string.cancel), (dialogInterface, i) -> {
})
.setPositiveButton(getString(R.string.discard), (dialogInterface, i) -> finish()).show();
} else {
finish();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.OpenableColumns;
Expand Down Expand Up @@ -46,7 +47,7 @@
*/
public class SettingsActivity extends AppCompatActivity {

private ArrayList <RecycleViewItem> mData = new ArrayList<>();
private final ArrayList <RecycleViewItem> mData = new ArrayList<>();
private String mPath;

@SuppressLint({"UseCompatLoadingForDrawables", "SetTextI18n"})
Expand Down Expand Up @@ -141,18 +142,33 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
startActivity(stringView);
finish();
} else {
if (Utils.isStorageWritePermissionDenied(this)) {
ActivityCompat.requestPermissions(this, new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
Utils.showSnackbar(findViewById(android.R.id.content), getString(R.string.permission_denied_write_storage));
if (Build.VERSION.SDK_INT >= 30) {
new MaterialAlertDialogBuilder(this).setItems(getResources().getStringArray(
R.array.import_options_sdk30), (dialogInterface, i) -> {
switch (i) {
case 0:
Translator.insertString(this);
break;
case 1:
Translator.importStringFromURL(this);
break;
}
}).setOnDismissListener(dialogInterface -> {
}).show();
} else {
new MaterialAlertDialogBuilder(this).setItems(getResources().getStringArray(
R.array.import_options), (dialogInterface, i) -> {
switch (i) {
case 0:
Intent importString = new Intent(Intent.ACTION_GET_CONTENT);
importString.setType("text/*");
startActivityForResult(importString, 0);
if (Utils.isPermissionDenied(this)) {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Utils.showSnackbar(findViewById(android.R.id.content), getString(R.string.permission_denied_write_storage));
} else {
Intent importString = new Intent(Intent.ACTION_GET_CONTENT);
importString.setType("text/*");
startActivityForResult(importString, 0);
}
break;
case 1:
Translator.importStringFromURL(this);
Expand All @@ -171,8 +187,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
.setPositiveButton(getString(R.string.yes), (dialogInterface3, iv) -> {
new File(getFilesDir().toString() + "/strings.xml").delete();
Utils.restartApp(this);
})
.show();
}).show();
} else {
Utils.showSnackbar(findViewById(android.R.id.content), getString(R.string.import_string_snackbar));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ public int getItemCount() {
}

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private MaterialTextView description;
private AppCompatImageButton imageButton;
private MaterialCardView layoutCard;

private final AppCompatImageButton imageButton;
private final MaterialCardView layoutCard;
private final MaterialTextView description;

public ViewHolder(View view) {
super(view);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
*/

public class RecycleViewItem implements Serializable {
private String mTitle;
private String mDescription;
private Drawable mIcon;
private String mURL;

private final Drawable mIcon;
private final String mDescription, mTitle, mURL;

public RecycleViewItem(String title, String description, Drawable icon, String url) {
this.mTitle = title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public class RecycleViewSettingsAdapter extends RecyclerView.Adapter<RecycleViewSettingsAdapter.ViewHolder> {

private ArrayList<RecycleViewItem> data;
private final ArrayList<RecycleViewItem> data;

private static ClickListener mClickListener;

Expand Down Expand Up @@ -73,9 +73,8 @@ public int getItemCount() {
}

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private AppCompatImageView mIcon;
private MaterialTextView mTitle;
private MaterialTextView mDescription;
private final AppCompatImageView mIcon;
private final MaterialTextView mDescription, mTitle;

public ViewHolder(View view) {
super(view);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public int getItemCount() {
}

public static class ViewHolder extends RecyclerView.ViewHolder {
private MaterialTextView description;
private final MaterialTextView description;

public ViewHolder(View view) {
super(view);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;

import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;
Expand All @@ -23,11 +26,15 @@
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.sunilpaulmathew.translator.BuildConfig;
import com.sunilpaulmathew.translator.R;
import com.sunilpaulmathew.translator.activities.InsertStringActivity;
import com.sunilpaulmathew.translator.adapters.RecycleViewAdapter;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

/*
Expand All @@ -41,13 +48,7 @@ public class Translator {
public static String mFindText, mKeyText;

public static void saveString(Activity activity) {
if (Utils.isStorageWritePermissionDenied(activity)) {
ActivityCompat.requestPermissions(activity, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Utils.showSnackbar(activity.findViewById(android.R.id.content), activity.getString(R.string.permission_denied_write_storage));
return;
}
Utils.dialogEditText("strings-" + java.util.Locale.getDefault().getLanguage(), activity.getString(R.string.save),
Utils.dialogEditText("strings-" + Locale.getDefault().getLanguage(), activity.getString(R.string.save),
activity.findViewById(android.R.id.content), (dialogInterface2, iii) -> {},
text -> {
if (text.isEmpty()) {
Expand All @@ -59,17 +60,18 @@ public static void saveString(Activity activity) {
}
String mString = getExportPath() + "/" + text;
if (Utils.exist(getExportPath() + "/" + text)) {
String finalText = text;
new MaterialAlertDialogBuilder(activity)
.setMessage(activity.getString(R.string.save_string_replace, text))
.setNegativeButton(activity.getString(R.string.cancel), (dialogInterface, i) -> {
})
.setPositiveButton(activity.getString(R.string.replace), (dialogInterface, i) -> {
Utils.create(getStrings(activity), mString);
writeFile(finalText, activity);
})
.show();
return;
}
Utils.create(getStrings(activity), mString);
writeFile(text, activity);
new MaterialAlertDialogBuilder(activity)
.setMessage(activity.getString(R.string.save_string_message, mString))
.setNegativeButton(activity.getString(R.string.cancel), (dialogInterface, i) -> {
Expand Down Expand Up @@ -152,7 +154,11 @@ public static String getSpecialCharacters(String string) {
}

private static String getExportPath() {
return Environment.getExternalStorageDirectory().toString();
if (Build.VERSION.SDK_INT >= 30) {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
} else {
return Environment.getExternalStorageDirectory().toString();
}
}

public static List<String> getData(Context context) {
Expand All @@ -172,8 +178,37 @@ public static List<String> getData(Context context) {
return mData;
}

private static void writeFile(String name, Activity activity) {
if (Build.VERSION.SDK_INT >= 30) {
try {
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
values.put(MediaStore.MediaColumns.MIME_TYPE, "application/xml");
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
Uri uri = activity.getContentResolver().insert(MediaStore.Files.getContentUri("external"), values);
OutputStream outputStream = activity.getContentResolver().openOutputStream(uri);
outputStream.write(getStrings(activity).getBytes());
outputStream.close();
} catch (IOException ignored) {
}
} else {
if (Utils.isPermissionDenied(activity)) {
ActivityCompat.requestPermissions(activity, new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Utils.showSnackbar(activity.findViewById(android.R.id.content), activity.getString(R.string.permission_denied_write_storage));
return;
}
Utils.create(getStrings(activity), getExportPath() + "/" + name);
}
}

public static void insertString(Activity activity) {
Intent insertString = new Intent(activity, InsertStringActivity.class);
activity.startActivity(insertString);
}

public static void importStringFromURL(Activity activity) {
Utils.dialogEditText(null, activity.getString(R.string.import_string), mRecyclerView,
Utils.dialogEditText(null, activity.getString(R.string.import_string), activity.findViewById(android.R.id.content),
(dialogInterface1, i1) -> {
}, text -> {
if (text.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public static boolean isNetworkAvailable(Context context) {
* Taken and used almost as such from the following stackoverflow discussion
* Ref: https://stackoverflow.com/questions/7203668/how-permission-can-be-checked-at-runtime-without-throwing-securityexception
*/
public static boolean isStorageWritePermissionDenied(Context context) {
public static boolean isPermissionDenied(Context context) {
String permission = android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
int res = context.checkCallingOrSelfPermission(permission);
return (res != PackageManager.PERMISSION_GRANTED);
Expand Down
Loading

0 comments on commit 461bc1e

Please sign in to comment.