Files
wownerujo/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java
T
2017-11-22 18:34:28 +01:00

259 lines
9.8 KiB
Java

/*
* Copyright (c) 2017 m2049r
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.m2049r.xmrwallet.util;
import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import com.m2049r.xmrwallet.R;
import com.m2049r.xmrwallet.model.Wallet;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.Locale;
import javax.net.ssl.HttpsURLConnection;
import okhttp3.OkHttpClient;
public class Helper {
static private final String TAG = "Helper";
static private final String WALLET_DIR = "monerujo";
static public int DISPLAY_DIGITS_INFO = 5;
static public int DISPLAY_DIGITS_SHORT = 5;
static public File getStorageRoot(Context context) {
if (!isExternalStorageWritable()) {
String msg = context.getString(R.string.message_strorage_not_writable);
Log.e(TAG, msg);
throw new IllegalStateException(msg);
}
File dir = new File(Environment.getExternalStorageDirectory(), WALLET_DIR);
if (!dir.exists()) {
Log.i(TAG, "Creating " + dir.getAbsolutePath());
dir.mkdirs(); // try to make it
}
if (!dir.isDirectory()) {
String msg = "Directory " + dir.getAbsolutePath() + " does not exist.";
Log.e(TAG, msg);
throw new IllegalStateException(msg);
}
return dir;
}
static public final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
static public boolean getWritePermission(Activity context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_DENIED) {
Log.w(TAG, "Permission denied to WRITE_EXTERNAL_STORAGE - requesting it");
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
context.requestPermissions(permissions, PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
return false;
} else {
return true;
}
} else {
return true;
}
}
static public final int PERMISSIONS_REQUEST_CAMERA = 1;
static public boolean getCameraPermission(Activity context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (context.checkSelfPermission(Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED) {
Log.w(TAG, "Permission denied for CAMERA - requesting it");
String[] permissions = {Manifest.permission.CAMERA};
context.requestPermissions(permissions, PERMISSIONS_REQUEST_CAMERA);
return false;
} else {
return true;
}
} else {
return true;
}
}
static public File getWalletFile(Context context, String aWalletName) {
File walletDir = getStorageRoot(context);
File f = new File(walletDir, aWalletName);
Log.d(TAG, "wallet = " + f.getAbsolutePath() + " size=" + f.length());
return f;
}
/* Checks if external storage is available for read and write */
private static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
static public void showKeyboard(Activity act) {
InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(act.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);
}
static public void hideKeyboard(Activity act) {
if (act == null) return;
if (act.getCurrentFocus() == null) {
act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
} else {
InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow((null == act.getCurrentFocus()) ? null : act.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
static public void showKeyboard(Dialog dialog) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
static public void hideKeyboardAlways(Activity act) {
act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
static public String getDisplayAmount(long amount) {
return getDisplayAmount(amount, 20);
}
static public String getDisplayAmount(long amount, int maxDecimals) {
return getDisplayAmount(Wallet.getDisplayAmount(amount), maxDecimals);
}
// amountString must have '.' as decimal point
private static String getDisplayAmount(String amountString, int maxDecimals) {
int lastZero = 0;
int decimal = 0;
for (int i = amountString.length() - 1; i >= 0; i--) {
if ((lastZero == 0) && (amountString.charAt(i) != '0')) lastZero = i + 1;
// TODO i18n
if (amountString.charAt(i) == '.') {
decimal = i + 1;
break;
}
}
int cutoff = Math.min(Math.max(lastZero, decimal + 2), decimal + maxDecimals);
return amountString.substring(0, cutoff);
}
static public String getFormattedAmount(double amount, boolean isXmr) {
// at this point selection is XMR in case of error
String displayB;
if (isXmr) { // not XMR
displayB = String.format(Locale.US, "%,.5f", amount);
} else { // XMR
displayB = String.format(Locale.US, "%,.2f", amount);
}
return displayB;
}
static public Bitmap getBitmap(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (drawable instanceof BitmapDrawable) {
return BitmapFactory.decodeResource(context.getResources(), drawableId);
} else if (drawable instanceof VectorDrawable) {
return getBitmap((VectorDrawable) drawable);
} else {
throw new IllegalArgumentException("unsupported drawable type");
}
}
static private Bitmap getBitmap(VectorDrawable vectorDrawable) {
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
return bitmap;
}
static final int HTTP_TIMEOUT = 5000;
static public String getUrl(String httpsUrl) {
HttpsURLConnection urlConnection = null;
try {
URL url = new URL(httpsUrl);
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setConnectTimeout(HTTP_TIMEOUT);
urlConnection.setReadTimeout(HTTP_TIMEOUT);
InputStreamReader in = new InputStreamReader(urlConnection.getInputStream());
StringBuffer sb = new StringBuffer();
final int BUFFER_SIZE = 512;
char[] buffer = new char[BUFFER_SIZE];
int length = in.read(buffer, 0, BUFFER_SIZE);
while (length >= 0) {
sb.append(buffer, 0, length);
length = in.read(buffer, 0, BUFFER_SIZE);
}
return sb.toString();
} catch (SocketTimeoutException ex) {
Log.w(TAG, "C " + ex.getLocalizedMessage());
} catch (MalformedURLException ex) {
Log.e(TAG, "A " + ex.getLocalizedMessage());
} catch (IOException ex) {
Log.e(TAG, "B " + ex.getLocalizedMessage());
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
static public void clipBoardCopy(Context context, String label, String text) {
ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboardManager.setPrimaryClip(clip);
}
static private OkHttpClient OkHttpClientSingleton;
static public final OkHttpClient getOkHttpClient() {
if (OkHttpClientSingleton == null) {
synchronized (Helper.class) {
if (OkHttpClientSingleton == null) {
OkHttpClientSingleton = new OkHttpClient();
}
}
}
return OkHttpClientSingleton;
}
}