Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.contactslist.util;
     18 
     19 import android.annotation.TargetApi;
     20 import android.os.Build;
     21 import android.os.StrictMode;
     22 
     23 import com.example.android.contactslist.ui.ContactDetailActivity;
     24 import com.example.android.contactslist.ui.ContactsListActivity;
     25 
     26 /**
     27  * This class contains static utility methods.
     28  */
     29 public class Utils {
     30 
     31     // Prevents instantiation.
     32     private Utils() {}
     33 
     34     /**
     35      * Enables strict mode. This should only be called when debugging the application and is useful
     36      * for finding some potential bugs or best practice violations.
     37      */
     38     @TargetApi(11)
     39     public static void enableStrictMode() {
     40         // Strict mode is only available on gingerbread or later
     41         if (Utils.hasGingerbread()) {
     42 
     43             // Enable all thread strict mode policies
     44             StrictMode.ThreadPolicy.Builder threadPolicyBuilder =
     45                     new StrictMode.ThreadPolicy.Builder()
     46                             .detectAll()
     47                             .penaltyLog();
     48 
     49             // Enable all VM strict mode policies
     50             StrictMode.VmPolicy.Builder vmPolicyBuilder =
     51                     new StrictMode.VmPolicy.Builder()
     52                             .detectAll()
     53                             .penaltyLog();
     54 
     55             // Honeycomb introduced some additional strict mode features
     56             if (Utils.hasHoneycomb()) {
     57                 // Flash screen when thread policy is violated
     58                 threadPolicyBuilder.penaltyFlashScreen();
     59                 // For each activity class, set an instance limit of 1. Any more instances and
     60                 // there could be a memory leak.
     61                 vmPolicyBuilder
     62                         .setClassInstanceLimit(ContactsListActivity.class, 1)
     63                         .setClassInstanceLimit(ContactDetailActivity.class, 1);
     64             }
     65 
     66             // Use builders to enable strict mode policies
     67             StrictMode.setThreadPolicy(threadPolicyBuilder.build());
     68             StrictMode.setVmPolicy(vmPolicyBuilder.build());
     69         }
     70     }
     71 
     72     /**
     73      * Uses static final constants to detect if the device's platform version is Gingerbread or
     74      * later.
     75      */
     76     public static boolean hasGingerbread() {
     77         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
     78     }
     79 
     80     /**
     81      * Uses static final constants to detect if the device's platform version is Honeycomb or
     82      * later.
     83      */
     84     public static boolean hasHoneycomb() {
     85         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
     86     }
     87 
     88     /**
     89      * Uses static final constants to detect if the device's platform version is Honeycomb MR1 or
     90      * later.
     91      */
     92     public static boolean hasHoneycombMR1() {
     93         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1;
     94     }
     95 
     96     /**
     97      * Uses static final constants to detect if the device's platform version is ICS or
     98      * later.
     99      */
    100     public static boolean hasICS() {
    101         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
    102     }
    103 }
    104