2016-03-03 17:46:31 +01:00
|
|
|
/*
|
2017-02-24 18:47:14 +01:00
|
|
|
This file is part of the dandelion*.
|
2016-03-03 17:46:31 +01:00
|
|
|
|
2017-02-24 18:47:14 +01:00
|
|
|
dandelion* is free software: you can redistribute it and/or modify
|
2016-03-03 17:46:31 +01:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
2017-02-24 18:47:14 +01:00
|
|
|
dandelion* is distributed in the hope that it will be useful,
|
2016-03-03 17:46:31 +01:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2017-02-24 18:47:14 +01:00
|
|
|
along with the dandelion*.
|
2016-03-03 17:46:31 +01:00
|
|
|
|
|
|
|
If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-03-29 19:38:50 +02:00
|
|
|
package com.github.dfa.diaspora_android.util;
|
2016-03-03 17:46:31 +01:00
|
|
|
|
2016-08-07 23:37:02 +02:00
|
|
|
import android.content.Context;
|
2016-06-04 03:48:57 +02:00
|
|
|
import android.content.Intent;
|
2016-09-18 23:17:18 +02:00
|
|
|
import android.content.res.Resources;
|
2016-08-07 23:37:02 +02:00
|
|
|
import android.net.Uri;
|
2016-09-18 23:17:18 +02:00
|
|
|
import android.os.Build;
|
2016-09-05 20:42:57 +02:00
|
|
|
import android.os.Bundle;
|
2016-08-27 12:34:08 +02:00
|
|
|
import android.os.Environment;
|
2017-03-25 16:45:32 +01:00
|
|
|
import android.support.annotation.RawRes;
|
|
|
|
import android.support.annotation.StringRes;
|
2016-10-05 02:30:10 +02:00
|
|
|
import android.support.design.widget.Snackbar;
|
2017-03-25 16:45:32 +01:00
|
|
|
import android.support.v4.content.ContextCompat;
|
|
|
|
import android.support.v7.app.AlertDialog;
|
|
|
|
import android.text.Html;
|
|
|
|
import android.text.SpannableString;
|
2017-03-05 20:51:56 +01:00
|
|
|
import android.text.TextUtils;
|
2017-03-25 16:45:32 +01:00
|
|
|
import android.text.method.LinkMovementMethod;
|
|
|
|
import android.util.TypedValue;
|
2016-10-05 02:30:10 +02:00
|
|
|
import android.view.View;
|
2017-03-25 16:45:32 +01:00
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.ScrollView;
|
|
|
|
import android.widget.TextView;
|
2016-03-03 17:46:31 +01:00
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
import com.github.dfa.diaspora_android.R;
|
2016-10-26 16:23:14 +02:00
|
|
|
import com.github.dfa.diaspora_android.web.WebHelper;
|
2016-07-18 14:02:18 +02:00
|
|
|
|
2016-08-27 16:40:16 +02:00
|
|
|
import java.io.BufferedReader;
|
2016-08-27 12:34:08 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2016-08-27 16:40:16 +02:00
|
|
|
import java.io.InputStreamReader;
|
2016-08-27 12:34:08 +02:00
|
|
|
import java.text.SimpleDateFormat;
|
2016-09-05 20:42:57 +02:00
|
|
|
import java.util.Arrays;
|
2016-08-27 12:34:08 +02:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
2017-05-20 02:08:04 +02:00
|
|
|
import io.github.gsantner.opoc.util.SimpleMarkdownParser;
|
|
|
|
|
2016-03-03 17:46:31 +01:00
|
|
|
public class Helpers {
|
2016-09-18 23:17:18 +02:00
|
|
|
public static int getColorFromRessource(Context context, int ressourceId) {
|
|
|
|
Resources res = context.getResources();
|
|
|
|
if (Build.VERSION.SDK_INT >= 23) {
|
|
|
|
return res.getColor(ressourceId, context.getTheme());
|
|
|
|
} else {
|
|
|
|
return res.getColor(ressourceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-27 12:34:08 +02:00
|
|
|
public static File createImageFile() throws IOException {
|
|
|
|
// Create an image file name
|
|
|
|
String timeStamp = new SimpleDateFormat("dd-MM-yy_HH-mm", Locale.getDefault()).format(new Date());
|
|
|
|
String imageFileName = "JPEG_" + timeStamp + "_";
|
2016-10-05 02:30:10 +02:00
|
|
|
AppLog.d(Helpers.class, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
|
2016-08-27 12:34:08 +02:00
|
|
|
File storageDir = Environment.getExternalStoragePublicDirectory(
|
|
|
|
Environment.DIRECTORY_PICTURES);
|
2016-09-18 23:17:18 +02:00
|
|
|
return new File(
|
2016-09-07 23:12:17 +02:00
|
|
|
imageFileName + /* prefix */
|
2016-09-18 23:17:18 +02:00
|
|
|
".jpg", /* suffix */
|
2016-09-07 23:12:17 +02:00
|
|
|
storageDir.getAbsolutePath() /* directory */
|
2016-08-27 12:34:08 +02:00
|
|
|
);
|
|
|
|
}
|
2016-08-27 16:40:16 +02:00
|
|
|
|
2017-03-05 20:51:56 +01:00
|
|
|
public static Locale getLocaleByAndroidCode(String code) {
|
|
|
|
if (!TextUtils.isEmpty(code)) {
|
|
|
|
return code.contains("-r")
|
|
|
|
? new Locale(code.substring(0, 2), code.substring(4, 6)) // de-rAT
|
|
|
|
: new Locale(code); // de
|
|
|
|
}
|
|
|
|
return Locale.getDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-27 16:40:16 +02:00
|
|
|
public static String readTextfileFromRawRessource(Context context, int rawRessourceId, String linePrefix, String linePostfix) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2016-11-08 20:41:53 +01:00
|
|
|
String line;
|
2016-08-27 16:40:16 +02:00
|
|
|
BufferedReader br = null;
|
|
|
|
linePrefix = linePrefix == null ? "" : linePrefix;
|
|
|
|
linePostfix = linePostfix == null ? "" : linePostfix;
|
|
|
|
|
|
|
|
try {
|
|
|
|
br = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(rawRessourceId)));
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
sb.append(linePrefix);
|
|
|
|
sb.append(line);
|
|
|
|
sb.append(linePostfix);
|
|
|
|
sb.append("\n");
|
|
|
|
}
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
} finally {
|
|
|
|
if (br != null) {
|
|
|
|
try {
|
|
|
|
br.close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
2017-03-25 16:45:32 +01:00
|
|
|
public static String loadMarkdownFromRawForTextView(Context context, @RawRes int rawMdFile, String prepend) {
|
|
|
|
try {
|
|
|
|
return new SimpleMarkdownParser()
|
|
|
|
.parse(context.getResources().openRawResource(rawMdFile),
|
|
|
|
SimpleMarkdownParser.FILTER_ANDROID_TEXTVIEW, prepend)
|
|
|
|
.replaceColor("#000001", ContextCompat.getColor(context, R.color.accent))
|
|
|
|
.removeMultiNewlines().replaceBulletCharacter("*").getHtml();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void showDialogWithHtmlTextView(Context context, String html, @StringRes int resTitleId) {
|
|
|
|
LinearLayout layout = new LinearLayout(context);
|
|
|
|
TextView textView = new TextView(context);
|
|
|
|
textView.setMovementMethod(LinkMovementMethod.getInstance());
|
|
|
|
ScrollView root = new ScrollView(context);
|
|
|
|
int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20,
|
|
|
|
context.getResources().getDisplayMetrics());
|
|
|
|
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
|
|
|
|
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
|
|
|
|
layoutParams.setMargins(margin, 0, margin, 0);
|
|
|
|
layout.setLayoutParams(layoutParams);
|
|
|
|
|
|
|
|
layout.addView(textView);
|
|
|
|
root.addView(layout);
|
|
|
|
|
|
|
|
textView.setText(new SpannableString(Html.fromHtml(html)));
|
|
|
|
AlertDialog.Builder dialog = new AlertDialog.Builder(context)
|
|
|
|
.setPositiveButton(android.R.string.ok, null)
|
|
|
|
.setTitle(resTitleId)
|
|
|
|
.setView(root);
|
|
|
|
dialog.show();
|
2016-08-27 16:40:16 +02:00
|
|
|
}
|
2016-09-05 20:42:57 +02:00
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
public static String colorToHex(int color) {
|
|
|
|
return "#" + Integer.toHexString(color & 0x00ffffff);
|
|
|
|
}
|
|
|
|
|
2016-09-05 20:42:57 +02:00
|
|
|
public static void printBundle(Bundle savedInstanceState, String k) {
|
2016-09-18 23:17:18 +02:00
|
|
|
if (savedInstanceState != null) {
|
2016-09-05 20:42:57 +02:00
|
|
|
for (String key : savedInstanceState.keySet()) {
|
2016-10-05 02:30:10 +02:00
|
|
|
AppLog.d("SAVED", key + " is a key in the bundle " + k);
|
2016-09-05 20:42:57 +02:00
|
|
|
Object bun = savedInstanceState.get(key);
|
2016-09-18 23:17:18 +02:00
|
|
|
if (bun != null) {
|
2016-09-05 20:42:57 +02:00
|
|
|
if (bun instanceof Bundle) {
|
|
|
|
printBundle((Bundle) bun, k + "." + key);
|
|
|
|
} else if (bun instanceof byte[]) {
|
2016-10-05 02:30:10 +02:00
|
|
|
AppLog.d("SAVED", "Key: " + k + "." + key + ": " + Arrays.toString((byte[]) bun));
|
2016-09-05 20:42:57 +02:00
|
|
|
} else {
|
2016-10-05 02:30:10 +02:00
|
|
|
AppLog.d("SAVED", "Key: " + k + "." + key + ": " + bun.toString());
|
2016-09-05 20:42:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-05 02:30:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show Information if user is offline, returns true if is not connected to internet
|
|
|
|
*
|
|
|
|
* @param context Context
|
|
|
|
* @param anchor A view anchor
|
|
|
|
*/
|
|
|
|
public static boolean showInfoIfUserNotConnectedToInternet(Context context, View anchor) {
|
|
|
|
boolean isOnline = WebHelper.isOnline(context);
|
|
|
|
if (!isOnline) {
|
|
|
|
Snackbar.make(anchor, R.string.no_internet, Snackbar.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
return !isOnline;
|
|
|
|
}
|
2016-11-08 13:07:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an Intent that opens url in any browser
|
2016-12-18 15:07:23 +01:00
|
|
|
*
|
2016-11-08 13:07:38 +01:00
|
|
|
* @param context context
|
2016-12-18 15:07:23 +01:00
|
|
|
* @param url url
|
2016-11-08 13:07:38 +01:00
|
|
|
*/
|
|
|
|
public static void openInExternalBrowser(Context context, String url) {
|
|
|
|
Intent openBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
|
|
|
|
openBrowserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
context.startActivity(openBrowserIntent);
|
|
|
|
}
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|