/* * ---------------------------------------------------------------------------- * * Gregor Santner wrote this file. You can do whatever * you want with this stuff. If we meet some day, and you think this stuff is * worth it, you can buy me a coke in return. Provided as is without any kind * of warranty. No attribution required. - Gregor Santner * * License: Creative Commons Zero (CC0 1.0) * http://creativecommons.org/publicdomain/zero/1.0/ * ---------------------------------------------------------------------------- */ package io.github.gsantner.opoc.util; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; 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; import android.text.method.ScrollingMovementMethod; import android.util.TypedValue; import android.view.inputmethod.InputMethodManager; import com.github.dfa.diaspora_android.R; @SuppressWarnings({"WeakerAccess", "unused", "SameParameterValue"}) public class HelpersA extends Helpers { protected Activity activity; protected HelpersA(Activity activity) { super(activity); this.activity = activity; } public static HelpersA get(Activity activity) { return new HelpersA(activity); } //######################## //## Methods //######################## /** * Animate to specified Activity * * @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 */ public void animateToActivity(Class to, Boolean finishFromActivity, Integer requestCode) { animateToActivity(new Intent(activity, to), finishFromActivity, requestCode); } /** * Animate to activity specified in intent * * @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 */ public void animateToActivity(Intent intent, Boolean finishFromActivity, Integer requestCode) { intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); if (requestCode != null) { activity.startActivityForResult(intent, requestCode); } else { activity.startActivity(intent); } activity.overridePendingTransition(R.anim.fadein, R.anim.fadeout); if (finishFromActivity != null && finishFromActivity) { activity.finish(); } } public void showSnackBar(@StringRes int stringId, boolean showLong) { Snackbar.make(activity.findViewById(android.R.id.content), stringId, showLong ? Snackbar.LENGTH_LONG : Snackbar.LENGTH_SHORT).show(); } public void hideSoftKeyboard() { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); if (activity.getCurrentFocus() != null && activity.getCurrentFocus().getWindowToken() != null) { inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); } } public void showDialogWithHtmlTextView(@StringRes int resTitleId, String html) { showDialogWithHtmlTextView(resTitleId, html, null); } public void showDialogWithHtmlTextView(@StringRes int resTitleId, String html, DialogInterface.OnDismissListener dismissedListener) { AppCompatTextView textView = new AppCompatTextView(context); int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, context.getResources().getDisplayMetrics()); textView.setMovementMethod(new ScrollingMovementMethod()); textView.setPadding(padding, 0, padding, 0); textView.setText(new SpannableString(Html.fromHtml(html))); AlertDialog.Builder dialog = new AlertDialog.Builder(context) .setPositiveButton(android.R.string.ok, null) .setOnDismissListener(dismissedListener) .setTitle(resTitleId) .setView(textView); dialog.show(); } }