Refactor some small classes into a Tools class

This commit is contained in:
Jakob Nixdorf 2017-10-13 07:35:10 +02:00
parent 149fc0a679
commit 44912927e0
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
5 changed files with 39 additions and 51 deletions

View file

@ -35,8 +35,8 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.shadowice.flocke.andotp.Utilities.ThemeHelper;
import org.shadowice.flocke.andotp.R;
import org.shadowice.flocke.andotp.Utilities.Tools;
import de.psdev.licensesdialog.LicensesDialog;
@ -77,7 +77,7 @@ public class AboutActivity extends BaseActivity {
stub.setLayoutResource(R.layout.content_about);
View v = stub.inflate();
ColorFilter filter = ThemeHelper.getThemeColorFilter(this, android.R.attr.textColorSecondary);
ColorFilter filter = Tools.getThemeColorFilter(this, android.R.attr.textColorSecondary);
for (int i : imageResources) {
ImageView imgView = v.findViewById(i);
imgView.getDrawable().setColorFilter(filter);

View file

@ -51,7 +51,7 @@ import org.shadowice.flocke.andotp.Database.Entry;
import org.shadowice.flocke.andotp.Utilities.FileHelper;
import org.shadowice.flocke.andotp.Utilities.DatabaseHelper;
import org.shadowice.flocke.andotp.Utilities.EncryptionHelper;
import org.shadowice.flocke.andotp.Utilities.StorageHelper;
import org.shadowice.flocke.andotp.Utilities.Tools;
import org.shadowice.flocke.andotp.R;
import java.io.ByteArrayInputStream;
@ -338,7 +338,7 @@ public class BackupActivity extends BaseActivity {
/* Plain-text backup functions */
private void doRestorePlain(Uri uri) {
if (StorageHelper.isExternalStorageReadable()) {
if (Tools.isExternalStorageReadable()) {
boolean success = DatabaseHelper.importFromJSON(this, uri);
if (success) {
@ -355,7 +355,7 @@ public class BackupActivity extends BaseActivity {
}
private void doBackupPlain(Uri uri) {
if (StorageHelper.isExternalStorageWritable()) {
if (Tools.isExternalStorageWritable()) {
boolean success = DatabaseHelper.exportAsJSON(this, uri);
if (success)
@ -395,7 +395,7 @@ public class BackupActivity extends BaseActivity {
String password = settings.getBackupPasswordEnc();
if (! password.isEmpty()) {
if (StorageHelper.isExternalStorageReadable()) {
if (Tools.isExternalStorageReadable()) {
boolean success = true;
try {
@ -431,7 +431,7 @@ public class BackupActivity extends BaseActivity {
String password = settings.getBackupPasswordEnc();
if (! password.isEmpty()) {
if (StorageHelper.isExternalStorageWritable()) {
if (Tools.isExternalStorageWritable()) {
ArrayList<Entry> entries = DatabaseHelper.loadDatabase(this);
String plain = DatabaseHelper.entriesToString(entries);
@ -465,7 +465,7 @@ public class BackupActivity extends BaseActivity {
/* OpenPGP backup functions */
private void doRestoreEncrypted(String content) {
if (StorageHelper.isExternalStorageReadable()) {
if (Tools.isExternalStorageReadable()) {
ArrayList<Entry> entries = DatabaseHelper.stringToEntries(content);
if (entries.size() > 0) {
@ -498,7 +498,7 @@ public class BackupActivity extends BaseActivity {
}
private void doBackupEncrypted(Uri uri, String data) {
if (StorageHelper.isExternalStorageWritable()) {
if (Tools.isExternalStorageWritable()) {
boolean success = FileHelper.writeStringToFile(this, uri, data);
if (success)

View file

@ -1,39 +0,0 @@
/*
* Copyright (C) 2017 Jakob Nixdorf
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.shadowice.flocke.andotp.Utilities;
import android.os.Environment;
public class StorageHelper {
/* Checks if external storage is available for read and write */
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
/* Checks if external storage is available to at least read */
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}
}

View file

@ -23,13 +23,39 @@
package org.shadowice.flocke.andotp.Utilities;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.os.Environment;
public class ThemeHelper {
import java.util.List;
public class Tools {
/* Checks if external storage is available for read and write */
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
/* Checks if external storage is available to at least read */
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}
/* Check is there is a handler for an Intent */
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return resolveInfo.size() > 0;
}
/* Get a color based on the current theme */
public static int getThemeColor(Context context, int colorAttr) {
Resources.Theme theme = context.getTheme();
TypedArray arr = theme.obtainStyledAttributes(new int[]{colorAttr});
@ -40,6 +66,7 @@ public class ThemeHelper {
return colorValue;
}
/* Create a ColorFilter based on the current theme */
public static ColorFilter getThemeColorFilter(Context context, int colorAttr) {
return new PorterDuffColorFilter(getThemeColor(context, colorAttr), PorterDuff.Mode.SRC_IN);
}

View file

@ -33,7 +33,7 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.shadowice.flocke.andotp.Utilities.ThemeHelper;
import org.shadowice.flocke.andotp.Utilities.Tools;
import org.shadowice.flocke.andotp.View.ItemTouchHelper.ItemTouchHelperViewHolder;
import org.shadowice.flocke.andotp.R;
@ -73,7 +73,7 @@ public class EntryViewHolder extends RecyclerView.ViewHolder
ImageView invisibleImg = v.findViewById(R.id.coverImg);
// Style the buttons in the current theme colors
ColorFilter colorFilter = ThemeHelper.getThemeColorFilter(context, android.R.attr.textColorSecondary);
ColorFilter colorFilter = Tools.getThemeColorFilter(context, android.R.attr.textColorSecondary);
menuButton.getDrawable().setColorFilter(colorFilter);
copyButton.getDrawable().setColorFilter(colorFilter);