Skip to content

Commit

Permalink
Fixed compatibility with Android version lower than 10
Browse files Browse the repository at this point in the history
  • Loading branch information
Electric1447 committed Aug 9, 2020
1 parent c79a1d9 commit 86797dc
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 32 deletions.
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
buildToolsVersion '30.0.0'

defaultConfig {
applicationId "eparon.vhbb_android"
minSdkVersion 21
targetSdkVersion 29
versionCode 11
versionName "beta-9"
versionCode 12
versionName "beta-10"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
resConfigs "en"
Expand All @@ -32,7 +32,7 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'
Expand All @@ -42,7 +42,7 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.preference:preference:1.1.1'

implementation 'com.google.android.material:material:1.1.0'
implementation 'com.google.android.material:material:1.2.0'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'jp.wasabeef:picasso-transformations:2.2.1'
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/eparon/vhbb_android/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package eparon.vhbb_android;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
Expand All @@ -19,6 +23,7 @@
import eparon.vhbb_android.Constants.VHBBAndroid;
import eparon.vhbb_android.Constants.VitaDB;
import eparon.vhbb_android.Utils.NetworkUtils;
import eparon.vhbb_android.Utils.PermissionUtils;

public class MainActivity extends AppCompatActivity {

Expand Down Expand Up @@ -60,7 +65,10 @@ protected void onCreate (Bundle savedInstanceState) {
});

if (!NetworkUtils.isNetworkAvailable(getApplicationContext()))
Toast.makeText(this, "Network not available", Toast.LENGTH_SHORT).show();
Toast.makeText(this, getString(R.string.err_network_not_available), Toast.LENGTH_SHORT).show();

if ((ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) && (Build.VERSION.SDK_INT < 29))
PermissionUtils.requestStoragePermission(this);
}

@Override
Expand Down
19 changes: 16 additions & 3 deletions app/src/main/java/eparon/vhbb_android/Utils/DownloadUtils.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
package eparon.vhbb_android.Utils;

import android.Manifest;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import eparon.vhbb_android.Constants.VitaDB;
import eparon.vhbb_android.R;

public class DownloadUtils {

public static void VHBBDownloadManager (Context context, Uri uri, String filename) {
public static void VHBBDownloadManager (Activity activity, Context context, Uri uri, String filename) {
if ((ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) && (Build.VERSION.SDK_INT < 29)) {
PermissionUtils.requestStoragePermission(activity);
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
return;
}

if (!NetworkUtils.isNetworkAvailable(context)) {
Toast.makeText(context, "Network not available", Toast.LENGTH_SHORT).show();
Toast.makeText(context, context.getString(R.string.err_network_not_available), Toast.LENGTH_SHORT).show();
return;
}

DownloadManager downloadmanager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);

DownloadManager.Request request = new DownloadManager.Request(uri)
.setTitle(filename)
.setDescription("Downloading...")
.setDescription(context.getString(R.string.downloading))
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setVisibleInDownloadsUi(true)
.addRequestHeader(VitaDB.UA_REQUEST_HEADER, VitaDB.UA_REQUEST_VALUE) // Set a valid user-agent for the requests.
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/java/eparon/vhbb_android/Utils/PermissionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package eparon.vhbb_android.Utils;

import android.Manifest;
import android.app.Activity;

import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;

import eparon.vhbb_android.R;

/**
* Permission Utilities
*/
public class PermissionUtils {

private static final int STORAGE_PERMISSION_CODE = 42069; // App's private request storage permission code.

/**
* Requests WRITE_EXTERNAL_STORAGE permission on runtime.
*
* @param activity the given activity
*/
public static void requestStoragePermission (Activity activity) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE))
new AlertDialog.Builder(activity)
.setTitle(activity.getString(R.string.permutils_title))
.setMessage(activity.getString(R.string.permutils_message))
.setPositiveButton(activity.getString(R.string.ok), (dialog, which) -> ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE))
.setNegativeButton(activity.getString(R.string.cancel), (dialog, which) -> dialog.dismiss())
.create().show();
else
ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eparon.vhbb_android.ui.cbpsdb;

import android.app.Activity;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -24,10 +25,12 @@

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

private Activity mActivity;
private ArrayList<CBPSDBItem> mCBPSDBList;
private ArrayList<CBPSDBItem> mCBPSDBListFull;

public CBPSDBAdapter (ArrayList<CBPSDBItem> cbpsdbList) {
public CBPSDBAdapter (Activity activity, ArrayList<CBPSDBItem> cbpsdbList) {
this.mActivity = activity;
this.mCBPSDBList = cbpsdbList;
this.mCBPSDBListFull = new ArrayList<>(mCBPSDBList);
}
Expand Down Expand Up @@ -84,11 +87,11 @@ public void onBindViewHolder (@NonNull CBPSDBAdapter.ViewHolder holder, int posi
filename = idID + ".vpk";
} else {
filename = idID;
Toast.makeText(v.getContext(), "Error while parsing file extension", Toast.LENGTH_SHORT).show();
Toast.makeText(v.getContext(), v.getContext().getString(R.string.err_parse_file_extension), Toast.LENGTH_SHORT).show();
}
}

DownloadUtils.VHBBDownloadManager(v.getContext(), Uri.parse(urlID), filename);
DownloadUtils.VHBBDownloadManager(mActivity, v.getContext(), Uri.parse(urlID), filename);
});

holder.mDownloadData.setVisibility(!dataUrlID.equals("None") ? View.VISIBLE : View.GONE);
Expand All @@ -97,7 +100,7 @@ public void onBindViewHolder (@NonNull CBPSDBAdapter.ViewHolder holder, int posi
String filename = urlID.substring(urlID.lastIndexOf("/") + 1);
filename = filename.substring(0, filename.lastIndexOf(".")) + "-data.zip";

DownloadUtils.VHBBDownloadManager(v.getContext(), Uri.parse(dataUrlID), filename);
DownloadUtils.VHBBDownloadManager(mActivity, v.getContext(), Uri.parse(dataUrlID), filename);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private void initializeAdapter (List<String[]> result) {
if (dataList.get(i)[0].equals(mCBPSDBList.get(j).getName()))
mCBPSDBList.get(j).setDataUrl(dataList.get(i)[1]);

mCBPSDBAdapter = new CBPSDBAdapter(mCBPSDBList);
mCBPSDBAdapter = new CBPSDBAdapter(requireActivity(), mCBPSDBList);
mRecyclerView.setAdapter(mCBPSDBAdapter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void onBindViewHolder (@NonNull CustomRepoAdapter.ViewHolder holder, int
holder.mDate.setText(String.format("(%s)", dateID));
holder.mDate.setVisibility(!dateID.equals("") ? View.VISIBLE : View.GONE);

holder.mDownload.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(v.getContext(), Uri.parse(currentItem.getUrl()), currentItem.getFilename()));
holder.mDownload.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(mActivity, v.getContext(), Uri.parse(currentItem.getUrl()), currentItem.getFilename()));

holder.mDownloadData.setVisibility(!dataUrlID.equals("") ? View.VISIBLE : View.GONE);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)holder.mDescription.getLayoutParams();
Expand All @@ -61,7 +61,7 @@ public void onBindViewHolder (@NonNull CustomRepoAdapter.ViewHolder holder, int
0);
holder.mDescription.setLayoutParams(lp);

if (!dataUrlID.equals("")) holder.mDownloadData.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(v.getContext(), Uri.parse(dataUrlID), currentItem.getDataFilename()));
if (!dataUrlID.equals("")) holder.mDownloadData.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(mActivity, v.getContext(), Uri.parse(dataUrlID), currentItem.getDataFilename()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ public View onCreateView (@NonNull LayoutInflater inflater, ViewGroup container,
private void showDialog () {
mCardView.setVisibility(View.GONE);
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext(), R.style.repoAlertDialogStyle);
builder.setTitle("Repository URL:");
builder.setTitle(getString(R.string.customrepo_dialog_title));

final EditText input = new EditText(requireContext());
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
builder.setView(input);

builder.setPositiveButton("OK", (dialog, which) -> jsonParse(input.getText().toString()));
builder.setNegativeButton("Cancel", (dialog, which) -> {
builder.setPositiveButton(getString(R.string.ok), (dialog, which) -> jsonParse(input.getText().toString()));
builder.setNegativeButton(getString(R.string.cancel), (dialog, which) -> {
dialog.cancel();
mCardView.setVisibility(View.VISIBLE);
});
Expand Down Expand Up @@ -114,7 +114,7 @@ private void jsonParse (String repoUrl) {
}
}, error -> {
mCardView.setVisibility(View.VISIBLE);
Toast.makeText(getContext(), "Invalid Repo URL", Toast.LENGTH_SHORT).show();
Toast.makeText(getContext(), getString(R.string.err_repo_invalid_url), Toast.LENGTH_SHORT).show();
});
mQueue.add(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void onBindViewHolder (@NonNull ExtrasAdapter.ViewHolder holder, int posi
holder.mDate.setText(String.format("(%s)", dateID));
holder.mDate.setVisibility(!dateID.equals("") ? View.VISIBLE : View.GONE);

holder.mDownload.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(v.getContext(), Uri.parse(currentItem.getUrl()), currentItem.getFilename()));
holder.mDownload.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(mActivity, v.getContext(), Uri.parse(currentItem.getUrl()), currentItem.getFilename()));

holder.mDownloadData.setVisibility(!dataUrlID.equals("") ? View.VISIBLE : View.GONE);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)holder.mDescription.getLayoutParams();
Expand All @@ -65,7 +65,7 @@ public void onBindViewHolder (@NonNull ExtrasAdapter.ViewHolder holder, int posi
0);
holder.mDescription.setLayoutParams(lp);

if (!dataUrlID.equals("")) holder.mDownloadData.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(v.getContext(), Uri.parse(dataUrlID), currentItem.getDataFilename()));
if (!dataUrlID.equals("")) holder.mDownloadData.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(mActivity, v.getContext(), Uri.parse(dataUrlID), currentItem.getDataFilename()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void onBindViewHolder (@NonNull ViewHolder holder, int position) {
holder.mDownloads.setText(String.format(Locale.getDefault(), "%dDLs", currentItem.getDownloads()));
Picasso.get().load(currentItem.getIconUrl()).fit().centerInside().into(holder.mIcon);

holder.mDownload.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(v.getContext(), Uri.parse(currentItem.getUrl()), currentItem.getName() + ".vpk"));
holder.mDownload.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(mActivity, v.getContext(), Uri.parse(currentItem.getUrl()), currentItem.getName() + ".vpk"));

String dataUrlID = currentItem.getDataUrl();

Expand All @@ -67,7 +67,7 @@ public void onBindViewHolder (@NonNull ViewHolder holder, int position) {
0);
holder.mDescription.setLayoutParams(lp);

if (!dataUrlID.equals("")) holder.mDownloadData.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(v.getContext(), Uri.parse(dataUrlID), dataUrlID.substring(dataUrlID.lastIndexOf("/") + 1)));
if (!dataUrlID.equals("")) holder.mDownloadData.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(mActivity, v.getContext(), Uri.parse(dataUrlID), dataUrlID.substring(dataUrlID.lastIndexOf("/") + 1)));

holder.mContainer.setOnClickListener(v -> {
Intent detailsIntent = new Intent(mActivity, HomebrewDetails.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ protected void onCreate (Bundle savedInstanceState) {
mSourceBtn.setOnClickListener(v -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(SourceUrl))));
mReleaseBtn.setOnClickListener(v -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(ReleaseUrl))));

mDownload.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(this, Uri.parse(cItem.getUrl()), cItem.getName() + ".vpk"));
mDownload.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(this, this, Uri.parse(cItem.getUrl()), cItem.getName() + ".vpk"));

mDownloadData.setVisibility(!DataUrl.equals("") ? View.VISIBLE : View.GONE);
if (!DataUrl.equals("")) mDownloadData.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(this, Uri.parse(DataUrl), DataUrl.substring(DataUrl.lastIndexOf("/") + 1)));
if (!DataUrl.equals("")) mDownloadData.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(this, this, Uri.parse(DataUrl), DataUrl.substring(DataUrl.lastIndexOf("/") + 1)));

if (ScreenshotsUrl != null)
if (ScreenshotsUrl.length == 1) Picasso.get().load(ScreenshotsUrl[0]).fit().centerInside().memoryPolicy(MemoryPolicy.NO_CACHE).into(mScreenshot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eparon.vhbb_android.ui.plugins;

import android.app.Activity;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -18,10 +19,12 @@

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

private Activity mActivity;
private ArrayList<PluginsItem> mPluginsList;
private ArrayList<PluginsItem> mPluginsListFull;

public PluginsAdapter (ArrayList<PluginsItem> pluginsList) {
public PluginsAdapter (Activity activity, ArrayList<PluginsItem> pluginsList) {
this.mActivity = activity;
this.mPluginsList = pluginsList;
this.mPluginsListFull = new ArrayList<>(mPluginsList);
}
Expand All @@ -41,7 +44,7 @@ public void onBindViewHolder (@NonNull ViewHolder holder, int position) {
holder.mAuthor.setText(currentItem.getAuthor());
holder.mDescription.setText(currentItem.getDescription());

holder.mDownload.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(v.getContext(), Uri.parse(currentItem.getUrl()), currentItem.getFilename()));
holder.mDownload.setOnClickListener(v -> DownloadUtils.VHBBDownloadManager(mActivity, v.getContext(), Uri.parse(currentItem.getUrl()), currentItem.getFilename()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private void jsonParse () {
mPluginsList.add(new PluginsItem(name, filename, version, author, description, url));
}

mPluginsAdapter = new PluginsAdapter(mPluginsList);
mPluginsAdapter = new PluginsAdapter(requireActivity(), mPluginsList);
mRecyclerView.setAdapter(mPluginsAdapter);
} catch (JSONException e) {
e.printStackTrace();
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<string name="nav_header_subtitle">Created by Itai Levin (Electric1447)</string>
<string name="nav_header_desc">Navigation header</string>

<string name="err_network_not_available">Network not available</string>
<string name="err_repo_invalid_url">Invalid Repo URL</string>
<string name="err_parse_file_extension">Error while parsing file extension</string>

<string name="menu_homebrew">Homebrew</string>
<string name="menu_homebrew_vitadb">Homebrew (VitaDB)</string>
<string name="menu_plugins">Plugins</string>
Expand All @@ -29,9 +33,16 @@

<string name="customrepo_refresh_title">Please enter a valid repo url.</string>
<string name="customrepo_refresh">Refresh</string>
<string name="customrepo_dialog_title">Repository URL:</string>

<string name="prefs_cache_title">Clear Cache</string>
<string name="prefs_cache_summery">Clear the application\'s cache.</string>

<string name="ok">OK</string>
<string name="cancel">Cancel</string>
<string name="about">About</string>
<string name="downloading">Downloading…</string>

<string name="permutils_title">Permission needed</string>
<string name="permutils_message">Storage permission is needed to download files</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath "com.android.tools.build:gradle:4.0.1"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Jun 01 15:25:01 IDT 2020
#Sun Aug 09 14:34:03 IDT 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-all.zip

0 comments on commit 86797dc

Please sign in to comment.