2017-05-29 19:05:37 +02:00
|
|
|
/*
|
2017-08-24 13:34:32 +02:00
|
|
|
* ------------------------------------------------------------------------------
|
2017-10-31 18:12:02 +01:00
|
|
|
* Gregor Santner <gsantner.net> wrote this. You can do whatever you want
|
2017-08-24 13:34:32 +02:00
|
|
|
* with it. If we meet some day, and you think it is worth it, you can buy me a
|
|
|
|
* coke in return. Provided as is without any kind of warranty. Do not blame or
|
|
|
|
* sue me if something goes wrong. No attribution required. - Gregor Santner
|
2017-05-29 19:05:37 +02:00
|
|
|
*
|
2017-08-24 13:34:32 +02:00
|
|
|
* License: Creative Commons Zero (CC0 1.0)
|
2017-05-29 19:05:37 +02:00
|
|
|
* http://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*/
|
2017-09-09 17:09:04 +02:00
|
|
|
package net.gsantner.opoc.util;
|
2017-05-29 19:05:37 +02:00
|
|
|
|
|
|
|
import android.app.Activity;
|
2017-11-18 20:04:59 +01:00
|
|
|
import android.content.ActivityNotFoundException;
|
2017-05-29 19:05:37 +02:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
2017-11-18 20:04:59 +01:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
2017-05-29 19:05:37 +02:00
|
|
|
import android.support.annotation.StringRes;
|
|
|
|
import android.support.design.widget.Snackbar;
|
|
|
|
import android.support.v7.app.AlertDialog;
|
|
|
|
import android.support.v7.widget.AppCompatTextView;
|
|
|
|
import android.text.Html;
|
|
|
|
import android.text.SpannableString;
|
2017-07-29 04:44:28 +02:00
|
|
|
import android.text.method.LinkMovementMethod;
|
2017-05-29 19:05:37 +02:00
|
|
|
import android.util.TypedValue;
|
2017-09-23 20:33:28 +02:00
|
|
|
import android.view.View;
|
2017-09-09 17:09:04 +02:00
|
|
|
import android.view.WindowManager;
|
2017-05-29 19:05:37 +02:00
|
|
|
import android.view.inputmethod.InputMethodManager;
|
|
|
|
|
|
|
|
|
2017-08-09 17:23:19 +02:00
|
|
|
@SuppressWarnings({"WeakerAccess", "unused", "SameParameterValue", "SpellCheckingInspection"})
|
2017-09-09 17:09:04 +02:00
|
|
|
public class ActivityUtils extends net.gsantner.opoc.util.ContextUtils {
|
2017-08-09 17:23:19 +02:00
|
|
|
//########################
|
|
|
|
//## Members, Constructors
|
|
|
|
//########################
|
|
|
|
protected Activity _activity;
|
2017-05-29 19:05:37 +02:00
|
|
|
|
2017-09-09 17:09:04 +02:00
|
|
|
public ActivityUtils(final Activity activity) {
|
2017-05-29 19:05:37 +02:00
|
|
|
super(activity);
|
2017-08-09 17:23:19 +02:00
|
|
|
_activity = activity;
|
2017-05-29 19:05:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//########################
|
|
|
|
//## Methods
|
|
|
|
//########################
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Animate to specified Activity
|
|
|
|
*
|
2017-08-24 13:34:32 +02:00
|
|
|
* @param to The class of the activity
|
|
|
|
* @param finishFromActivity true: Finish the current activity
|
|
|
|
* @param requestCode Request code for stating the activity, not waiting for result if null
|
2017-05-29 19:05:37 +02:00
|
|
|
*/
|
|
|
|
public void animateToActivity(Class to, Boolean finishFromActivity, Integer requestCode) {
|
2017-08-09 17:23:19 +02:00
|
|
|
animateToActivity(new Intent(_activity, to), finishFromActivity, requestCode);
|
2017-05-29 19:05:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 17:23:19 +02:00
|
|
|
* Animate to Activity specified in intent
|
|
|
|
* Requires animation resources
|
2017-05-29 19:05:37 +02:00
|
|
|
*
|
2017-08-24 13:34:32 +02:00
|
|
|
* @param intent Intent to open start an activity
|
|
|
|
* @param finishFromActivity true: Finish the current activity
|
|
|
|
* @param requestCode Request code for stating the activity, not waiting for result if null
|
2017-05-29 19:05:37 +02:00
|
|
|
*/
|
|
|
|
public void animateToActivity(Intent intent, Boolean finishFromActivity, Integer requestCode) {
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
|
|
|
|
if (requestCode != null) {
|
2017-08-09 17:23:19 +02:00
|
|
|
_activity.startActivityForResult(intent, requestCode);
|
2017-05-29 19:05:37 +02:00
|
|
|
} else {
|
2017-08-09 17:23:19 +02:00
|
|
|
_activity.startActivity(intent);
|
2017-05-29 19:05:37 +02:00
|
|
|
|
|
|
|
}
|
2017-08-09 17:23:19 +02:00
|
|
|
_activity.overridePendingTransition(getResId(ResType.DIMEN, "fadein"), getResId(ResType.DIMEN, "fadeout"));
|
2017-05-29 19:05:37 +02:00
|
|
|
if (finishFromActivity != null && finishFromActivity) {
|
2017-08-09 17:23:19 +02:00
|
|
|
_activity.finish();
|
2017-05-29 19:05:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-23 20:33:28 +02:00
|
|
|
public void showSnackBar(@StringRes int stringResId, boolean showLong) {
|
|
|
|
Snackbar.make(_activity.findViewById(android.R.id.content), stringResId,
|
2017-05-29 19:05:37 +02:00
|
|
|
showLong ? Snackbar.LENGTH_LONG : Snackbar.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
|
2017-09-23 20:33:28 +02:00
|
|
|
public void showSnackBar(@StringRes int stringResId, boolean showLong, @StringRes int actionResId, View.OnClickListener listener) {
|
|
|
|
Snackbar.make(_activity.findViewById(android.R.id.content), stringResId,
|
|
|
|
showLong ? Snackbar.LENGTH_LONG : Snackbar.LENGTH_SHORT)
|
|
|
|
.setAction(actionResId, listener)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
|
2017-05-29 19:05:37 +02:00
|
|
|
public void hideSoftKeyboard() {
|
2017-08-09 17:23:19 +02:00
|
|
|
InputMethodManager inputMethodManager = (InputMethodManager) _activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
|
|
|
|
if (_activity.getCurrentFocus() != null && _activity.getCurrentFocus().getWindowToken() != null) {
|
|
|
|
inputMethodManager.hideSoftInputFromWindow(_activity.getCurrentFocus().getWindowToken(), 0);
|
2017-05-29 19:05:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 20:04:59 +01:00
|
|
|
public void showSoftKeyboard() {
|
|
|
|
InputMethodManager inputMethodManager = (InputMethodManager) _activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
|
|
|
|
if (_activity.getCurrentFocus() != null && _activity.getCurrentFocus().getWindowToken() != null) {
|
|
|
|
inputMethodManager.showSoftInput(_activity.getCurrentFocus(), InputMethodManager.SHOW_FORCED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-29 19:05:37 +02:00
|
|
|
public void showDialogWithHtmlTextView(@StringRes int resTitleId, String html) {
|
2017-07-29 04:44:28 +02:00
|
|
|
showDialogWithHtmlTextView(resTitleId, html, true, null);
|
2017-05-29 19:05:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-29 04:44:28 +02:00
|
|
|
public void showDialogWithHtmlTextView(@StringRes int resTitleId, String text, boolean isHtml, DialogInterface.OnDismissListener dismissedListener) {
|
2017-08-09 17:23:19 +02:00
|
|
|
AppCompatTextView textView = new AppCompatTextView(_context);
|
2017-05-29 19:05:37 +02:00
|
|
|
int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16,
|
2017-08-09 17:23:19 +02:00
|
|
|
_context.getResources().getDisplayMetrics());
|
2017-07-29 04:44:28 +02:00
|
|
|
textView.setMovementMethod(new LinkMovementMethod());
|
2017-05-29 19:05:37 +02:00
|
|
|
textView.setPadding(padding, 0, padding, 0);
|
|
|
|
|
2017-07-29 04:44:28 +02:00
|
|
|
textView.setText(isHtml ? new SpannableString(Html.fromHtml(text)) : text);
|
2017-08-09 17:23:19 +02:00
|
|
|
AlertDialog.Builder dialog = new AlertDialog.Builder(_context)
|
2017-05-29 19:05:37 +02:00
|
|
|
.setPositiveButton(android.R.string.ok, null)
|
|
|
|
.setOnDismissListener(dismissedListener)
|
|
|
|
.setTitle(resTitleId)
|
|
|
|
.setView(textView);
|
|
|
|
dialog.show();
|
|
|
|
}
|
2017-09-09 17:09:04 +02:00
|
|
|
|
|
|
|
// Toggle with no param, else set visibility according to first bool
|
|
|
|
public void toggleStatusbarVisibility(boolean... optionalForceVisible) {
|
|
|
|
WindowManager.LayoutParams attrs = _activity.getWindow().getAttributes();
|
|
|
|
int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
|
|
|
|
if (optionalForceVisible.length == 0) {
|
|
|
|
attrs.flags ^= flag;
|
|
|
|
} else if (optionalForceVisible.length == 1 && optionalForceVisible[0]) {
|
|
|
|
attrs.flags &= ~flag;
|
|
|
|
} else {
|
|
|
|
attrs.flags |= flag;
|
|
|
|
}
|
|
|
|
_activity.getWindow().setAttributes(attrs);
|
|
|
|
}
|
2017-11-18 20:04:59 +01:00
|
|
|
|
|
|
|
public void showRateOnGplayDialog() {
|
|
|
|
String pkgId = "details?id=" + _activity.getPackageName();
|
|
|
|
Intent goToMarket = new Intent(Intent.ACTION_VIEW, Uri.parse("market://" + pkgId));
|
|
|
|
goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
|
|
|
|
(Build.VERSION.SDK_INT >= 21 ? Intent.FLAG_ACTIVITY_NEW_DOCUMENT : Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) |
|
|
|
|
Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
|
|
|
|
try {
|
|
|
|
_activity.startActivity(goToMarket);
|
|
|
|
} catch (ActivityNotFoundException e) {
|
|
|
|
_activity.startActivity(new Intent(Intent.ACTION_VIEW,
|
|
|
|
Uri.parse("http://play.google.com/store/apps/" + pkgId)));
|
|
|
|
}
|
|
|
|
}
|
2017-05-29 19:05:37 +02:00
|
|
|
}
|