Java tutorial
/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package air.com.snagfilms.utils; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Map; import java.util.TimeZone; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; import air.com.snagfilms.Analytics.AnalyticsAPI; import air.com.snagfilms.app.Controller; import air.com.snagfilms.constants.Constants; import android.annotation.TargetApi; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Point; import android.net.Uri; import android.os.Build; import android.os.Build.VERSION_CODES; import android.os.StrictMode; import android.provider.Settings.Secure; import android.util.TypedValue; import android.view.ContextThemeWrapper; import android.view.Display; import android.view.WindowManager; import com.google.analytics.tracking.android.Fields; import com.google.analytics.tracking.android.MapBuilder; import com.snagfilms.filmrise.R; /** * Class containing some static utility methods. */ public class Utils { // staging keys final static String ACCESS_KEY = "XKITMPXAHGNRKVPVCREU"; final static String SECRET_KEY = "NQUkBQTBsjYXWFeUL9KMiwwjyoKxMzavVqot6zFf18FcT"; private Utils() { }; @TargetApi(VERSION_CODES.HONEYCOMB) public static void enableStrictMode() { if (Utils.hasGingerbread()) { StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder().detectAll() .penaltyLog(); StrictMode.VmPolicy.Builder vmPolicyBuilder = new StrictMode.VmPolicy.Builder().detectAll() .penaltyLog(); if (Utils.hasHoneycomb()) { threadPolicyBuilder.penaltyFlashScreen(); /* * vmPolicyBuilder * .setClassInstanceLimit(ImageGridActivity.class, 1) * .setClassInstanceLimit(ImageDetailActivity.class, 1); */ } StrictMode.setThreadPolicy(threadPolicyBuilder.build()); StrictMode.setVmPolicy(vmPolicyBuilder.build()); } } public static boolean hasFroyo() { // Can use static final constants like FROYO, declared in later versions // of the OS since they are inlined at compile time. This is guaranteed // behavior. return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO; } public static boolean hasGingerbread() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD; } public static boolean hasHoneycomb() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB; } public static boolean hasHoneycombMR1() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1; } public static boolean hasJellyBean() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; } public static boolean hasKitKat() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; } public static boolean isTablet(Context context) { return context.getResources().getBoolean(R.bool.isTablet); } /* * * Given a URI, returns a map of campaign data that can be sent with any * GA hit. * * * @param uri A hierarchical URI that may or may not have campaign data * stored in query parameters. * * @return A map that may contain campaign or referrer that may be sent with * any Google Analytics hit. */ public static Map<String, String> getReferrerMapFromUri(Uri uri) { MapBuilder paramMap = new MapBuilder(); // If no URI, return an empty Map. if (uri == null) { return paramMap.build(); } // Source is the only required campaign field. No need to continue if // not // present. if (uri.getQueryParameter(AnalyticsAPI.CAMPAIGN_SOURCE_PARAM) != null) { // MapBuilder.setCampaignParamsFromUrl parses Google Analytics // campaign // ("UTM") parameters from a string URL into a Map that can be set // on // the Tracker. paramMap.setCampaignParamsFromUrl(uri.toString()); // If no source parameter, set authority to source and medium to // "referral". } else if (uri.getAuthority() != null) { paramMap.set(Fields.CAMPAIGN_MEDIUM, "referral"); paramMap.set(Fields.CAMPAIGN_SOURCE, uri.getAuthority()); } return paramMap.build(); } public static AlertDialog showConfirmationDialog(String title, Context mContext) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( new ContextThemeWrapper(mContext, R.style.AlertDialogCustom)); if (title != null) { alertDialogBuilder.setMessage(title); } else { alertDialogBuilder.setMessage("INFO:"); } alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); return alertDialogBuilder.create(); } public static int convertpxToDp(Context context, int px) { int dp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, px, context.getResources().getDisplayMetrics()); return dp; } public static String getDeviceParameters(Context ctx) { StringBuilder strBlr = null; String device_id = Secure.getString(ctx.getContentResolver(), Secure.ANDROID_ID); String PhoneModel = android.os.Build.MODEL; strBlr = new StringBuilder(); strBlr.append(Constants.DEVICE_ID); strBlr.append("="); strBlr.append(device_id); strBlr.append("&"); strBlr.append(Constants.DEVICE_MODEL); strBlr.append("="); strBlr.append(PhoneModel); return strBlr.toString(); } public static String getfilmriseParameters() { StringBuilder strBlr = null; try { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); long longs = System.currentTimeMillis(); String timestamp = dateFormat.format(longs); ; String stringToSign = timestamp; // Query uses this string // Compute the signature and base64 encode it. String algorithm = "HmacSHA1"; SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), algorithm); Mac mac = Mac.getInstance(algorithm); mac.init(key); String signature = new String(Base64.encodeBase64(mac.doFinal(stringToSign.getBytes()))); System.out.println(signature);// required for Query signature = URLEncoder.encode(signature, "UTF-8"); strBlr = new StringBuilder(); strBlr.append(Constants.ACCESS_KEY); strBlr.append("="); strBlr.append(ACCESS_KEY); strBlr.append("&"); strBlr.append(Constants.TIME_STAMP); strBlr.append("="); strBlr.append(timestamp); strBlr.append("&"); strBlr.append(Constants.SIGNATURE); strBlr.append("="); strBlr.append(signature); strBlr.append("&"); strBlr.append(Constants.SITE); strBlr.append("="); strBlr.append(Constants.filmrise); strBlr.append("&"); strBlr.append(Constants.DEVICE); strBlr.append("="); strBlr.append("android"); return strBlr.toString(); } catch (Exception e) { } return null; } public static Point getScreenSize(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); display.getSize(size); return size; } public static int getSizeInDp(Context context, int sizeInPx, float density) { int dp = (int) (sizeInPx / context.getResources().getDisplayMetrics().density + density); return dp; } public static boolean isCineroar() { return (Constants.APP_FILMRISE.equals(Controller.app_name)); } }