Home | History | Annotate | Download | only in util
      1 /*
      2  *  Copyright 2014 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 package org.appspot.apprtc.util;
     12 
     13 import android.os.Build;
     14 import android.util.Log;
     15 
     16 /**
     17  * AppRTCUtils provides helper functions for managing thread safety.
     18  */
     19 public final class AppRTCUtils {
     20 
     21   private AppRTCUtils() {
     22   }
     23 
     24   /**
     25    * NonThreadSafe is a helper class used to help verify that methods of a
     26    * class are called from the same thread.
     27    */
     28   public static class NonThreadSafe {
     29     private final Long threadId;
     30 
     31     public NonThreadSafe() {
     32       // Store thread ID of the creating thread.
     33       threadId = Thread.currentThread().getId();
     34     }
     35 
     36    /** Checks if the method is called on the valid/creating thread. */
     37     public boolean calledOnValidThread() {
     38        return threadId.equals(Thread.currentThread().getId());
     39     }
     40   }
     41 
     42   /** Helper method which throws an exception  when an assertion has failed. */
     43   public static void assertIsTrue(boolean condition) {
     44     if (!condition) {
     45       throw new AssertionError("Expected condition to be true");
     46     }
     47   }
     48 
     49   /** Helper method for building a string of thread information.*/
     50   public static String getThreadInfo() {
     51     return "@[name=" + Thread.currentThread().getName()
     52         + ", id=" + Thread.currentThread().getId() + "]";
     53   }
     54 
     55   /** Information about the current build, taken from system properties. */
     56   public static void logDeviceInfo(String tag) {
     57     Log.d(tag, "Android SDK: " + Build.VERSION.SDK_INT + ", "
     58         + "Release: " + Build.VERSION.RELEASE + ", "
     59         + "Brand: " + Build.BRAND + ", "
     60         + "Device: " + Build.DEVICE + ", "
     61         + "Id: " + Build.ID + ", "
     62         + "Hardware: " + Build.HARDWARE + ", "
     63         + "Manufacturer: " + Build.MANUFACTURER + ", "
     64         + "Model: " + Build.MODEL + ", "
     65         + "Product: " + Build.PRODUCT);
     66   }
     67 }
     68