Skip to content

Commit

Permalink
Squashed 'libs/utils/' changes from f75a9e7..5b22440
Browse files Browse the repository at this point in the history
5b22440 Merge branch 'release/3.2' into develop
1a2a73f fix debug/release build propagation to sub projects
d9eac2e Merge branch 'release/3.2' into develop
380ece6 Merge pull request #1861 from wordpress-mobile/feature/notifications-redesign
c38f131 Merge pull request #1860 from wordpress-mobile/issue/1821-remove-istablet
4e92eaa Merge branch 'hotfix/3.1.2' into release/3.2
cbc7b24 fix #1821: remove DisplayUtis.isTablet() method
343771a Catch NPE in Bitmap.CreateBitmap and make sure the returned bitmap is not null
c44a87a Merge branch 'release/3.2' into feature/notifications-redesign
97c557c Revert "Revert "Merge pull request #1799 from wordpress-mobile/feature/1795-reader-render-attachments""
1cacaf2 Merge branch 'hotfix/3.1.1' into release/3.2
568782f Merge branch 'hotfix/3.1.1' into develop
96fd180 remove isTablet() check
870944d Merge branch 'release/3.2' into feature/notifications-redesign
3390393 Revert "Merge pull request #1799 from wordpress-mobile/feature/1795-reader-render-attachments"
03bbac0 Merge branch 'develop' into feature/notifications-redesign
8ec2915 Replaced String.format() with string concatenation in ReaderUtils and PhotonUtils after profiler showed String.format() to be hurting performance
1623d65 fix lint error in utils
e273dd3 Merge branch 'develop' into feature/notifications-redesign
4bfc641 Removed aggressive `JSONUtil` logging
f7f4556 update support-v13 library to version 19.1.0
fafcf67 Merge pull request #1781 from wordpress-mobile/issue/17760-reader-comment-full-images
1868cc5 disable refresh on the attacher
082c13f use the PTR library version 0.9.7
f21f5d3 set network refresh mode as default
d262f8d remove unused code
9e592fd add a network mode in PullToRefreshHeaderTransformer to show a different message when network is disabled
f85a003 move NetworkUtils to WPUtils subtree
d19c080 Updated comment
cabb8a7 TextView content is reset to itself to force it to correctly resize using cached image
48e4ed2 More cleanup
2426243 Cleanup WPImageGetter based on code analysis
c955d26 Merge pull request #1764 from wordpress-mobile/issue/1713-send-version-code-to-mixpanel
3bc1080 Merge branch 'release/3.1' into develop
caa9db0 fix #1762: show blog url in the share blog spinner if the blog title is an empty string
e1eecbf remove ProfilingUtils.getVersionName calls
075b989 remove unused method from ProfilingUtils
b48cbda move BuildUtils to WPUtils subtree, rename BuildUtils to PackageUtils and add getPackageInfo and getVersionCode methods

git-subtree-dir: libs/utils
git-subtree-split: 5b22440
  • Loading branch information
maxme committed Sep 21, 2014
1 parent 3a77c01 commit 78d3966
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 102 deletions.
6 changes: 3 additions & 3 deletions WordPressUtils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ dependencies {
compile 'commons-lang:commons-lang:2.6'
compile 'com.mcxiaoke.volley:library:1.0.+'
compile 'com.github.castorflex.smoothprogressbar:library:0.4.0'
compile 'org.wordpress:pulltorefresh-main:+@aar' // org.wordpress version includes some fixes
compile 'com.android.support:support-v13:19.0.+'
compile 'org.wordpress:pulltorefresh-main:0.9.7@aar' // org.wordpress version includes some fixes
compile 'com.android.support:support-v13:19.1.0'
}

android {
defaultPublishConfig 'debug'
publishNonDefault true

compileSdkVersion 19
buildToolsVersion "19.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public static ArrayList<String> toHtmlList(Context context) {
ArrayList<String> items = new ArrayList<String>();

// add version & device info - be sure to change HEADER_LINE_COUNT if additional lines are added
items.add("<strong>WordPress Android version: " + ProfilingUtils.getVersionName(context) + "</strong>");
items.add("<strong>WordPress Android version: " + PackageUtils.getVersionName(context) + "</strong>");
items.add("<strong>Android device name: " + DeviceUtils.getInstance().getDeviceName(context) + "</strong>");

Iterator<LogEntry> it = mLogEntries.iterator();
Expand All @@ -193,7 +193,7 @@ public static String toPlainText(Context context) {
StringBuilder sb = new StringBuilder();

// add version & device info
sb.append("WordPress Android version: " + ProfilingUtils.getVersionName(context)).append("\n")
sb.append("WordPress Android version: " + PackageUtils.getVersionName(context)).append("\n")
.append("Android device name: " + DeviceUtils.getInstance().getDeviceName(context)).append("\n\n");

Iterator<LogEntry> it = mLogEntries.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@ public int compare(Object blog1, Object blog2) {
return blogName1.compareToIgnoreCase(blogName2);
}
};

/**
* Return a blog name or blog url (host part only) if trimmed name is an empty string
*/
public static String getBlogNameFromAccountMap(Map<String, Object> account) {
String blogName = StringUtils.unescapeHTML(MapUtils.getMapStr(account, "blogName"));
if (blogName.trim().length() == 0) {
blogName = StringUtils.getHost(MapUtils.getMapStr(account, "url"));
}
return blogName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public static boolean isLandscape(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
}

public static boolean isLandscapeTablet(Context context) {
return isLandscape(context) && isTablet(context);
}

public static Point getDisplayPixelSize(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Expand All @@ -43,7 +39,8 @@ public static int getDisplayPixelHeight(Context context) {
}

public static int dpToPx(Context context, int dp) {
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
context.getResources().getDisplayMetrics());
return (int) px;
}

Expand All @@ -52,16 +49,11 @@ public static int pxToDp(Context context, int px) {
return (int) ((px/displayMetrics.density)+0.5);
}

public static boolean isTablet(Context context) {
// http://stackoverflow.com/a/8427523/1673548
if (context == null)
return false;
return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

public static boolean isXLarge(Context context) {
if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE)
if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
== Configuration.SCREENLAYOUT_SIZE_XLARGE) {
return true;
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,16 +444,17 @@ public static byte[] createThumbnailFromUri(Context context,
optActual.inSampleSize = scale;

// Get the roughly resized bitmap
Bitmap bmpResized;
final Bitmap bmpResized;
try {
bmpResized = BitmapFactory.decodeFile(filePath, optActual);
} catch (OutOfMemoryError e) {
AppLog.e(AppLog.T.UTILS, "OutOfMemoryError Error in setting image: " + e);
return null;
}

if (bmpResized == null)
if (bmpResized == null) {
return null;
}

ByteArrayOutputStream stream = new ByteArrayOutputStream();

Expand Down Expand Up @@ -490,7 +491,18 @@ public static byte[] createThumbnailFromUri(Context context,
} catch (OutOfMemoryError e) {
AppLog.e(AppLog.T.UTILS, "OutOfMemoryError Error in setting image: " + e);
return null;
} catch (NullPointerException e) {
// See: https://github.com/wordpress-mobile/WordPress-Android/issues/1844
AppLog.e(AppLog.T.UTILS, "Bitmap.createBitmap has thrown a NPE internally. This should never happen: " + e);
return null;
}

if (bmpRotated == null) {
// Fix an issue where bmpRotated is null even if the documentation doesn't say Bitmap.createBitmap can return null.
// See: https://github.com/wordpress-mobile/WordPress-Android/issues/1848
return null;
}

bmpRotated.compress(fmt, 100, stream);
bmpResized.recycle();
bmpRotated.recycle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject)
AppLog.e(T.UTILS, "Unable to cast the object to " + defaultObject.getClass().getName(), e);
return defaultObject;
} catch (JSONException e) {
AppLog.e(T.UTILS, "Unable to get the Key from the input object. Key:" + query, e);
return defaultObject;
}
}
Expand Down Expand Up @@ -133,7 +132,6 @@ public static <U> U queryJSON(JSONArray source, String query, U defaultObject){
AppLog.e(T.UTILS, "Unable to cast the object to "+defaultObject.getClass().getName(), e);
return defaultObject;
} catch (JSONException e) {
AppLog.e(T.UTILS, "Unable to get the Key from the input object. Key:" + query, e);
return defaultObject;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.wordpress.android.util;

import android.annotation.TargetApi;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.provider.Settings;

/**
* requires android.permission.ACCESS_NETWORK_STATE
*/

public class NetworkUtils {
public static final int TYPE_UNKNOWN = -1;

/**
* returns information on the active network connection
*/
private static NetworkInfo getActiveNetworkInfo(Context context) {
if (context == null) {
return null;
}
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return null;
}
// note that this may return null if no network is currently active
return cm.getActiveNetworkInfo();
}

/**
* returns the ConnectivityManager.TYPE_xxx if there's an active connection, otherwise
* returns TYPE_UNKNOWN
*/
private static int getActiveNetworkType(Context context) {
NetworkInfo info = getActiveNetworkInfo(context);
if (info == null || !info.isConnected()) {
return TYPE_UNKNOWN;
}
return info.getType();
}

/**
* returns true if a network connection is available
*/
public static boolean isNetworkAvailable(Context context) {
NetworkInfo info = getActiveNetworkInfo(context);
return (info != null && info.isConnected());
}

/**
* returns true if the user is connected to WiFi
*/
public static boolean isWiFiConnected(Context context) {
return (getActiveNetworkType(context) == ConnectivityManager.TYPE_WIFI);
}

/**
* returns true if airplane mode has been enabled
*/
@TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings("deprecation")
public static boolean isAirplaneModeOn(Context context) {
// prior to JellyBean 4.2 this was Settings.System.AIRPLANE_MODE_ON, JellyBean 4.2
// moved it to Settings.Global
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0;
} else {
return Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}

/**
* returns true if there's an active network connection, otherwise displays a toast error
* and returns false
*/
public static boolean checkConnection(Context context) {
if (isNetworkAvailable(context)) {
return true;
}
ToastUtils.showToast(context, R.string.no_network_message);
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.wordpress.android.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class PackageUtils {
/**
* Return true if Debug build. false otherwise.
*/
public static boolean isDebugBuild() {
return BuildConfig.DEBUG;
}

public static PackageInfo getPackageInfo(Context context) {
try {
PackageManager manager = context.getPackageManager();
return manager.getPackageInfo(context.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
}

/**
* Return version code, or 0 if it can't be read
*/
public static int getVersionCode(Context context) {
PackageInfo packageInfo = getPackageInfo(context);
if (packageInfo != null) {
return packageInfo.versionCode;
}
return 0;
}

/**
* Return version name, or the string "0" if it can't be read
*/
public static String getVersionName(Context context) {
PackageInfo packageInfo = getPackageInfo(context);
if (packageInfo != null) {
return packageInfo.versionName;
}
return "0";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static String fixAvatar(final String imageUrl, int avatarSz) {
return getPhotonImageUrl(imageUrl, avatarSz, avatarSz);

// remove all other params, then add query string for size and "mystery man" default
return UrlUtils.removeQuery(imageUrl) + String.format("?s=%d&d=mm", avatarSz);
return UrlUtils.removeQuery(imageUrl) + "?s=" + avatarSz + "&d=mm";
}

/*
Expand Down Expand Up @@ -64,18 +64,18 @@ public static String getPhotonImageUrl(String imageUrl, int width, int height) {
// see http://wp.tutsplus.com/tutorials/how-to-generate-website-screenshots-for-your-wordpress-site/
// ex: http://s.wordpress.com/mshots/v1/http%3A%2F%2Fnickbradbury.com?w=600
if (isMshotsUrl(imageUrl)) {
return imageUrl + String.format("?w=%d&h=%d", width, height);
return imageUrl + "?w=" + width + "&h=" + height;
}

// if both width & height are passed use the "resize" param, use only "w" or "h" if just
// one of them is set, otherwise no query string
final String query;
if (width > 0 && height > 0) {
query = String.format("?resize=%d,%d", width, height);
query = "?resize=" + width + "," + height;
} else if (width > 0) {
query = String.format("?w=%d", width);
query = "?w=" + width;
} else if (height > 0) {
query = String.format("?h=%d", height);
query = "?h=" + height;
} else {
query = "";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.wordpress.android.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.SystemClock;

import org.wordpress.android.util.AppLog.T;
Expand Down Expand Up @@ -76,16 +73,5 @@ public void dumpToLog() {
}
AppLog.d(T.PROFILING, mLabel + ": end, " + (now - first) + " ms");
}

// Returns app version name String
public static String getVersionName(Context context) {
PackageManager pm = context.getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
return pi.versionName == null ? "" : pi.versionName;
} catch (PackageManager.NameNotFoundException e) {
return "";
}
}
}

Loading

0 comments on commit 78d3966

Please sign in to comment.