package com.leavjenn.hews.misc; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.BitmapFactory; import android.graphics.Point; import android.net.ConnectivityManager; import android.net.Uri; import android.support.annotation.ColorRes; import android.support.annotation.StringRes; import android.support.customtabs.CustomTabsIntent; import android.support.design.widget.Snackbar; import android.text.Html; import android.text.format.DateUtils; import android.util.DisplayMetrics; import android.util.Log; import android.view.Display; import android.view.WindowManager; import android.webkit.URLUtil; import android.widget.TextView; import android.widget.Toast; import com.leavjenn.hews.Constants; import com.leavjenn.hews.R; import com.leavjenn.hews.misc.ShareBroadcastReceiver; import com.leavjenn.hews.misc.SharedPrefsManager; import com.leavjenn.hews.model.Post; public class Utils implements com.leavjenn.hews.misc.UtilsContract { private Context mContext; public Utils(Context context) { mContext = context; } public static CharSequence formatTime(long timeStamp) { timeStamp = timeStamp * 1000; CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(timeStamp, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS); return timeAgo; } public static float convertPixelsToDp(float px, Context context) { DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return px / (metrics.densityDpi / 160f); } public static int convertDpToPixels(int dp, Context context) { DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)); return px; } public static float convertSpToPixels(float sp, Context context) { float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity; return sp * scaledDensity; } public static int getScreenHeight(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); display.getSize(size); return size.y; } public static boolean isOnline(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); return connectivityManager.getActiveNetworkInfo() != null; } public static void showLongToast(Context context, CharSequence text) { Toast.makeText(context, text, Toast.LENGTH_LONG).show(); } public static void showLongToast(Context context, @StringRes int ResId) { Toast.makeText(context, ResId, Toast.LENGTH_LONG).show(); } public static void setSnackBarTextColor(Snackbar snackbar, Context context, @ColorRes int color) { TextView tvSnackbarText = (TextView) snackbar.getView() .findViewById(android.support.design.R.id.snackbar_text); tvSnackbarText.setTextColor(context.getResources().getColor(color)); } // uri scheme can only lower case char public static Uri validateAndParseUri(String uriString, long postId) { if (URLUtil.isValidUrl(uriString)) { // if (Patterns.WEB_URL.matcher(uriString).matches()) { if (uriString.startsWith("Http")) { Log.i("validateAndParseUri", "http" + uriString.substring(4)); return Uri.parse("http" + uriString.substring(4)); } else { // TODO other conditions return Uri.parse(uriString); } } else { return Uri.parse("https://news.ycombinator.com/item?id=" + postId); } // } } // setup url and pretty url public static void setupPostUrl(Post post) { String url = post.getUrl(); if (url == null || url.isEmpty()) { url = Constants.YCOMBINATOR_ITEM_URL + post.getId(); post.setUrl(url); } String[] splitUrl = url.split("/"); if (splitUrl.length > 2) { url = splitUrl[2]; post.setPrettyUrl(url); } } public static void setupIntentBuilder(CustomTabsIntent.Builder intentBuilder, Context context, SharedPreferences prefs) { if (SharedPrefsManager.getTheme(prefs).equals(SharedPrefsManager.THEME_DARK)) { intentBuilder.setToolbarColor(context.getResources().getColor(R.color.grey_900)); } else if (SharedPrefsManager.getTheme(prefs).equals(SharedPrefsManager.THEME_AMOLED_BLACK)) { intentBuilder.setToolbarColor(context.getResources().getColor(android.R.color.black)); } else { // use darker orange color here so chrome toolbar will fit dark theme intentBuilder.setToolbarColor(context.getResources().getColor(R.color.orange_800)); } intentBuilder.enableUrlBarHiding(); intentBuilder.setShowTitle(true); intentBuilder.setCloseButtonIcon( BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_arrow_back)); // share link option String menuItemTitle = context.getString(R.string.share_link_to); Intent actionIntent = new Intent(context.getApplicationContext(), ShareBroadcastReceiver.class); PendingIntent menuItemPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, actionIntent, 0); intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); // TODO Use this way, it will show share to option when long click // String shareLabel = getString(R.string.share_link_to); // Intent shareIntent = new Intent(); // shareIntent.setAction(Intent.ACTION_SEND); // String postUrl = post.getUrl(); // shareIntent.putExtra(Intent.EXTRA_TEXT, postUrl); // shareIntent.setType("text/plain"); // shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Intent chooserIntent = Intent.createChooser(shareIntent, getString(R.string.share_link_to)); // startActivity(chooserIntent); // PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, // shareIntent, 0); // intentBuilder.addMenuItem(shareLabel, pendingIntent); } @Override public boolean isOnline() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext .getSystemService(Context.CONNECTIVITY_SERVICE); return connectivityManager.getActiveNetworkInfo() != null; } @Override public String convertHtmlToString(String html) { return Html.fromHtml(html).toString(); } }