dandelion/app/src/main/java/net/gsantner/opoc/util/ActivityUtils.java

348 lines
14 KiB
Java
Raw Normal View History

2018-03-12 00:05:53 +01:00
/*#######################################################
2017-05-29 19:05:37 +02:00
*
2018-03-12 00:05:53 +01:00
* Maintained by Gregor Santner, 2016-
* https://gsantner.net/
*
2018-10-01 21:12:17 +02:00
* License of this file: Apache 2.0 (Commercial upon request)
* https://www.apache.org/licenses/LICENSE-2.0
* https://github.com/gsantner/opoc/#licensing
2018-03-12 00:05:53 +01:00
*
#########################################################*/
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;
2021-01-18 21:32:50 +01:00
import android.app.ActivityManager;
2017-11-18 20:04:59 +01:00
import android.content.ActivityNotFoundException;
2018-10-01 21:12:17 +02:00
import android.content.ComponentName;
2021-01-18 21:32:50 +01:00
import android.content.Context;
2017-05-29 19:05:37 +02:00
import android.content.DialogInterface;
import android.content.Intent;
2018-10-01 21:12:17 +02:00
import android.content.pm.PackageManager;
2019-07-26 03:19:28 +02:00
import android.content.res.TypedArray;
2017-11-18 20:04:59 +01:00
import android.net.Uri;
import android.os.Build;
2019-07-26 03:19:28 +02:00
import android.provider.CalendarContract;
import android.support.annotation.ColorInt;
2017-05-29 19:05:37 +02:00
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
2018-05-21 14:57:41 +02:00
import android.support.v4.content.ContextCompat;
2017-05-29 19:05:37 +02:00
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;
import android.view.Window;
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;
import android.webkit.WebView;
2019-01-11 02:38:30 +01:00
import android.widget.ScrollView;
2017-05-29 19:05:37 +02:00
2021-01-18 21:32:50 +01:00
import java.util.List;
2017-05-29 19:05:37 +02:00
@SuppressWarnings({"WeakerAccess", "unused", "SameParameterValue", "SpellCheckingInspection", "rawtypes", "UnusedReturnValue"})
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
}
2019-07-26 03:19:28 +02:00
@Override
public void freeContextRef() {
super.freeContextRef();
_activity = null;
}
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
}
}
2019-07-26 03:19:28 +02:00
public Snackbar showSnackBar(@StringRes int stringResId, boolean showLong) {
Snackbar s = Snackbar.make(_activity.findViewById(android.R.id.content), stringResId,
showLong ? Snackbar.LENGTH_LONG : Snackbar.LENGTH_SHORT);
s.show();
return s;
2017-05-29 19:05:37 +02:00
}
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();
}
2019-07-26 03:19:28 +02:00
public ActivityUtils setSoftKeyboardVisibile(boolean visible, View... editView) {
final Activity activity = _activity;
if (activity != null) {
final View v = (editView != null && editView.length > 0) ? (editView[0]) : (activity.getCurrentFocus() != null && activity.getCurrentFocus().getWindowToken() != null ? activity.getCurrentFocus() : null);
final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (v != null && imm != null) {
Runnable r = () -> {
if (visible) {
v.requestFocus();
imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);
} else {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
};
r.run();
for (int d : new int[]{100, 350}) {
v.postDelayed(r, d);
}
}
2017-05-29 19:05:37 +02:00
}
2019-07-26 03:19:28 +02:00
return this;
2017-05-29 19:05:37 +02:00
}
2019-07-26 03:19:28 +02:00
public ActivityUtils hideSoftKeyboard() {
if (_activity != null) {
InputMethodManager imm = (InputMethodManager) _activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null && _activity.getCurrentFocus() != null && _activity.getCurrentFocus().getWindowToken() != null) {
imm.hideSoftInputFromWindow(_activity.getCurrentFocus().getWindowToken(), 0);
}
2017-11-18 20:04:59 +01:00
}
2019-07-26 03:19:28 +02:00
return this;
}
public ActivityUtils showSoftKeyboard() {
if (_activity != null) {
InputMethodManager imm = (InputMethodManager) _activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null && _activity.getCurrentFocus() != null && _activity.getCurrentFocus().getWindowToken() != null) {
showSoftKeyboard(_activity.getCurrentFocus());
}
}
return this;
}
public ActivityUtils showSoftKeyboard(View textInputView) {
if (_activity != null) {
InputMethodManager imm = (InputMethodManager) _activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm != null && textInputView != null) {
imm.showSoftInput(textInputView, InputMethodManager.SHOW_FORCED);
}
}
return this;
2017-11-18 20:04:59 +01:00
}
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) {
2019-01-11 02:38:30 +01:00
ScrollView scroll = new ScrollView(_context);
2017-08-09 17:23:19 +02:00
AppCompatTextView textView = new AppCompatTextView(_context);
2019-01-11 02:38:30 +01:00
int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, _context.getResources().getDisplayMetrics());
2017-05-29 19:05:37 +02:00
2019-01-11 02:38:30 +01:00
scroll.setPadding(padding, 0, padding, 0);
scroll.addView(textView);
textView.setMovementMethod(new LinkMovementMethod());
2017-07-29 04:44:28 +02:00
textView.setText(isHtml ? new SpannableString(Html.fromHtml(text)) : text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
2019-01-11 02:38:30 +01:00
2017-08-09 17:23:19 +02:00
AlertDialog.Builder dialog = new AlertDialog.Builder(_context)
2019-01-11 02:38:30 +01:00
.setPositiveButton(android.R.string.ok, null).setOnDismissListener(dismissedListener)
.setView(scroll);
if (resTitleId != 0) {
dialog.setTitle(resTitleId);
}
dialogFullWidth(dialog.show(), true, false);
2017-05-29 19:05:37 +02:00
}
2017-09-09 17:09:04 +02:00
public void showDialogWithRawFileInWebView(String fileInRaw, @StringRes int resTitleId) {
WebView wv = new WebView(_context);
wv.loadUrl("file:///android_res/raw/" + fileInRaw);
AlertDialog.Builder dialog = new AlertDialog.Builder(_context)
.setPositiveButton(android.R.string.ok, null)
.setTitle(resTitleId)
.setView(wv);
dialogFullWidth(dialog.show(), true, false);
}
2017-09-09 17:09:04 +02:00
// Toggle with no param, else set visibility according to first bool
2019-07-26 03:19:28 +02:00
public ActivityUtils toggleStatusbarVisibility(boolean... optionalForceVisible) {
2017-09-09 17:09:04 +02:00
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);
2019-07-26 03:19:28 +02:00
return this;
2017-09-09 17:09:04 +02:00
}
2017-11-18 20:04:59 +01:00
2019-07-26 03:19:28 +02:00
public ActivityUtils showGooglePlayEntryForThisApp() {
2017-11-18 20:04:59 +01:00
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,
2019-01-11 02:38:30 +01:00
Uri.parse("https://play.google.com/store/apps/" + pkgId)));
2017-11-18 20:04:59 +01:00
}
2019-07-26 03:19:28 +02:00
return this;
2017-11-18 20:04:59 +01:00
}
2018-05-21 14:57:41 +02:00
2019-07-26 03:19:28 +02:00
public ActivityUtils setStatusbarColor(int color, boolean... fromRes) {
2018-05-21 14:57:41 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (fromRes != null && fromRes.length > 0 && fromRes[0]) {
color = ContextCompat.getColor(_context, color);
}
_activity.getWindow().setStatusBarColor(color);
}
2019-07-26 03:19:28 +02:00
return this;
2018-05-21 14:57:41 +02:00
}
2018-10-01 21:12:17 +02:00
2019-07-26 03:19:28 +02:00
public ActivityUtils setLauncherActivityEnabled(Class activityClass, boolean enable) {
try {
ComponentName component = new ComponentName(_context, activityClass);
_context.getPackageManager().setComponentEnabledSetting(component, enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
} catch (Exception ignored) {
}
2019-07-26 03:19:28 +02:00
return this;
}
public boolean isLauncherEnabled(Class activityClass) {
try {
ComponentName component = new ComponentName(_context, activityClass);
return _context.getPackageManager().getComponentEnabledSetting(component) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
} catch (Exception ignored) {
}
return false;
}
2019-07-26 03:19:28 +02:00
@ColorInt
public Integer getCurrentPrimaryColor() {
TypedValue typedValue = new TypedValue();
_context.getTheme().resolveAttribute(getResId(ResType.ATTR, "colorPrimary"), typedValue, true);
return typedValue.data;
}
@ColorInt
public Integer getCurrentPrimaryDarkColor() {
TypedValue typedValue = new TypedValue();
_context.getTheme().resolveAttribute(getResId(ResType.ATTR, "colorPrimaryDark"), typedValue, true);
return typedValue.data;
}
@ColorInt
public Integer getCurrentAccentColor() {
TypedValue typedValue = new TypedValue();
_context.getTheme().resolveAttribute(getResId(ResType.ATTR, "colorAccent"), typedValue, true);
return typedValue.data;
}
@ColorInt
public Integer getActivityBackgroundColor() {
TypedArray array = _activity.getTheme().obtainStyledAttributes(new int[]{
android.R.attr.colorBackground,
});
int c = array.getColor(0, 0xFF0000);
array.recycle();
return c;
}
public ActivityUtils startCalendarApp() {
Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
builder.appendPath(Long.toString(System.currentTimeMillis()));
Intent intent = new Intent(Intent.ACTION_VIEW, builder.build());
_activity.startActivity(intent);
return this;
2018-10-01 21:12:17 +02:00
}
/**
* Detect if the activity is currently in splitscreen/multiwindow mode
*/
public boolean isInSplitScreenMode() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return _activity.isInMultiWindowMode();
}
return false;
}
/**
* Show dialog in full width / show keyboard
*
* @param dialog Get via dialog.show()
*/
public void dialogFullWidth(AlertDialog dialog, boolean fullWidth, boolean showKeyboard) {
try {
Window w;
if (dialog != null && (w = dialog.getWindow()) != null) {
if (fullWidth) {
w.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
}
if (showKeyboard) {
w.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
} catch (Exception ignored) {
}
}
2021-01-18 21:32:50 +01:00
// Make activity/app not show up in the recents history - call before finish / System.exit
public ActivityUtils removeActivityFromHistory() {
try {
ActivityManager am = (ActivityManager) _activity.getSystemService(Context.ACTIVITY_SERVICE);
if (am != null && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
List<ActivityManager.AppTask> tasks = am.getAppTasks();
if (tasks != null && !tasks.isEmpty()) {
tasks.get(0).setExcludeFromRecents(true);
}
}
} catch (Exception ignored) {
}
return this;
}
2017-05-29 19:05:37 +02:00
}