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