Exemple #1
0
  /**
   * For when you don't care if the Closeable you're closing is null, and you don't care that
   * closing a non-null buffer threw an exception, but you still care enough that logging might be
   * useful, like in a "finally" block, after you've already returned to the caller.
   *
   * @param closeable
   */
  public static void closeThisThingOrMaybeDont(Closeable closeable) {
    if (closeable == null) {
      log.d("Can't close closable, arg was null.");
      return;
    }

    try {
      closeable.close();
    } catch (IOException e) {
      log.d("Couldn't close closable, but that's apparently OK.  Error was: " + e.getMessage());
    }
  }
 public void reenableWifiNetworks() {
   log.v("reenableWifiNetworks()");
   for (String ssid : loadSSIDStringSetWithKey(KEY_DISABLED_WIFI_SSIDS)) {
     WiFi.reenableNetwork(ssid, ctx);
   }
   saveSSIDsWithKey(KEY_DISABLED_WIFI_SSIDS, new HashSet<String>());
 }
Exemple #3
0
 public static void threadSleep(long millis) {
   try {
     Thread.sleep(millis);
   } catch (InterruptedException e) {
     log.e("Thread interrupted: ", e);
   }
 }
Exemple #4
0
  /**
   * 发送通知广播
   *
   * @param context
   * @param notice
   */
  public static void sendBroadCast(Context context, Notice notice, String from) {
    // if (!((AppContext) context.getApplicationContext()).isLogin()
    // || notice == null)
    // return;
    if (notice == null || context == null) return;
    // AppContext.setNoticeAtMeCount(notice.getAtmeCount());
    // AppContext.setNoticeMessageCount(notice.getMsgCount());
    // AppContext.setNoticeReviewCount(notice.getReviewCount());
    // AppContext.setNoticeNewFansCount(notice.getNewFansCount());

    TLog.log(
        "NoticeReceiver",
        "发送通知广播 from:"
            + from
            + " atme:"
            + notice.getAtmeCount()
            + " review:"
            + notice.getReviewCount()
            + " msg:"
            + notice.getMsgCount()
            + " fans:"
            + notice.getNewFansCount());
    Intent intent = new Intent(Constants.INTENT_ACTION_NOTICE);
    intent.putExtra("atmeCount", notice.getAtmeCount());
    intent.putExtra("msgCount", notice.getMsgCount());
    intent.putExtra("reviewCount", notice.getReviewCount());
    intent.putExtra("newFansCount", notice.getNewFansCount());
    intent.putExtra("from", from);
    context.sendBroadcast(intent);
  }
  private Set<String> loadSSIDStringSetWithKey(String key) {
    log.v("loadSSIDStringSetWithKey(" + key + ")");

    Set<String> ssids = set();
    ssids = prefs.getStringSet(key, ssids);
    log.v("Loaded saved SSIDS: " + ssids);

    Set<String> diffQuotes = set();
    for (String ssid : ssids) {
      diffQuotes.add(WiFi.enQuotifySsid(ssid));
      diffQuotes.add(WiFi.deQuotifySsid(ssid));
    }
    ssids.addAll(diffQuotes);

    log.v("Returning SSIDS: " + ssids);
    return ssids;
  }
  public static void main(String[] args) {
    System.out.println("** TestConstructor");
    XLog.initializeLogger();

    new Animal();
    new Duck();
    new RedDuck();
    new WhiteDuck();
    new DottedRedDuck();

    XLog.closeLogger();
    TLog.printDebug();
  }
  public static void main(String[] args) throws Exception {
    System.out.println("** TestPolymorph");
    TLog.initializeLogger();

    TestPolymorph z = new TestPolymorph();
    z.f1(new A());
    z.f1(new B1());
    z.f1(new B2());
    z.f1(new C());
    z.f1(new D());

    z.f2(new B1());
    z.f2(new C());
    z.f2(new D());

    z.f3(new D());

    DLog.initialize(TLog.getDebugLogCopy(), TLog.getDebugEventLogCopy());
    DLog.DEBUG = true;
    DLog.printEncodedLog();
    f1DEC();
    f1DEC();
    f1DEC();
    f1DEC();
    f1DEC();

    f2DEC();
    f2DEC();
    f2DEC();

    f3DEC();

    DLog.tick();
    DLog.closeDecoder();
    DLog.printDebug();
  }
 @SuppressLint("CommitPrefEdits")
 private void saveSSIDsWithKey(String key, Set<String> ssids) {
   log.v("saveSSIDsWithKey() " + key + ", " + ssids);
   prefs.edit().putStringSet(key, ssids).commit();
 }
 public void onWifiNetworkDisabled(String ssid) {
   log.v("onWifiNetworkDisabled() " + ssid);
   Set<String> ssids = set(loadSSIDStringSetWithKey(KEY_DISABLED_WIFI_SSIDS));
   ssids.add(ssid);
   saveSSIDsWithKey(KEY_DISABLED_WIFI_SSIDS, ssids);
 }
public class SoftAPConfigRemover {

  private static final TLog log = TLog.get(SoftAPConfigRemover.class);

  private static final String PREFS_SOFT_AP_NETWORK_REMOVER = "PREFS_SOFT_AP_NETWORK_REMOVER";

  private static final String KEY_SOFT_AP_SSIDS = "KEY_SOFT_AP_SSIDS";
  private static final String KEY_DISABLED_WIFI_SSIDS = "KEY_DISABLED_WIFI_SSIDS";

  private final Context ctx;
  private final SharedPreferences prefs;

  public SoftAPConfigRemover(Context ctx) {
    this.ctx = ctx.getApplicationContext();
    prefs = ctx.getSharedPreferences(PREFS_SOFT_AP_NETWORK_REMOVER, Context.MODE_PRIVATE);
  }

  public void onSoftApConfigured(String newSsid) {
    // make a defensive copy of what we get back
    Set<String> ssids = set(loadSSIDStringSetWithKey(KEY_SOFT_AP_SSIDS));
    ssids.add(newSsid);
    saveSSIDsWithKey(KEY_SOFT_AP_SSIDS, ssids);
  }

  public void removeAllSoftApConfigs() {
    for (String ssid : loadSSIDStringSetWithKey(KEY_SOFT_AP_SSIDS)) {
      WiFi.removeNetwork(ssid, ctx);
    }
    saveSSIDsWithKey(KEY_SOFT_AP_SSIDS, new HashSet<String>());
  }

  public void onWifiNetworkDisabled(String ssid) {
    log.v("onWifiNetworkDisabled() " + ssid);
    Set<String> ssids = set(loadSSIDStringSetWithKey(KEY_DISABLED_WIFI_SSIDS));
    ssids.add(ssid);
    saveSSIDsWithKey(KEY_DISABLED_WIFI_SSIDS, ssids);
  }

  public void reenableWifiNetworks() {
    log.v("reenableWifiNetworks()");
    for (String ssid : loadSSIDStringSetWithKey(KEY_DISABLED_WIFI_SSIDS)) {
      WiFi.reenableNetwork(ssid, ctx);
    }
    saveSSIDsWithKey(KEY_DISABLED_WIFI_SSIDS, new HashSet<String>());
  }

  private Set<String> loadSSIDStringSetWithKey(String key) {
    log.v("loadSSIDStringSetWithKey(" + key + ")");

    Set<String> ssids = set();
    ssids = prefs.getStringSet(key, ssids);
    log.v("Loaded saved SSIDS: " + ssids);

    Set<String> diffQuotes = set();
    for (String ssid : ssids) {
      diffQuotes.add(WiFi.enQuotifySsid(ssid));
      diffQuotes.add(WiFi.deQuotifySsid(ssid));
    }
    ssids.addAll(diffQuotes);

    log.v("Returning SSIDS: " + ssids);
    return ssids;
  }

  @SuppressLint("CommitPrefEdits")
  private void saveSSIDsWithKey(String key, Set<String> ssids) {
    log.v("saveSSIDsWithKey() " + key + ", " + ssids);
    prefs.edit().putStringSet(key, ssids).commit();
  }
}
 public void f2(B1 b) {
   // original is b.foo(0); this is the tagging:
   Class[] variants = {D.class, C.class, B1.class};
   ((B1) TLog.polycall(b, variants)).foo(0);
 }
 public void f1(A a) {
   // we just want to do a.foo(0); this gives the following tagging:
   Class[] variants = {D.class, C.class, B1.class, B2.class, A.class};
   ((A) TLog.polycall(a, variants)).foo(0);
 }
 public void foo(int x) {
   System.out.println("** calling foo of C");
   TLog.log("I am C.foo()");
 }
 public void foo(int x) {
   TLog.log("I am B2.foo()");
 }
Exemple #15
0
/**
 * Analgesic shortcuts for Android dev't.
 *
 * <p>Created by jensck on 11/22/14.
 */
public class EZ {

  private static final TLog log = TLog.get(EZ.class);

  public static void runOnMainThread(Runnable runnable) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(runnable);
  }

  public static void runOnMainThreadDelayed(long delayInMillis, Runnable runnable) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(runnable, delayInMillis);
  }

  public static void runAsync(final Runnable runnable) {
    new AsyncTask<Void, Void, Void>() {

      @Override
      protected Void doInBackground(Void... params) {
        runnable.run();
        return null;
      }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  }

  public static void threadSleep(long millis) {
    try {
      Thread.sleep(millis);
    } catch (InterruptedException e) {
      log.e("Thread interrupted: ", e);
    }
  }

  public static boolean isUsingOlderWifiStack() {
    return Build.VERSION.SDK_INT < 20;
  }

  /**
   * Return the callbacks for a fragment or throw an exception.
   *
   * <p>Inspired by: https://gist.github.com/keyboardr/5455206
   */
  @SuppressWarnings("unchecked")
  public static <T> T getCallbacksOrThrow(Fragment frag, Class<T> callbacks) {
    Fragment parent = frag.getParentFragment();

    if (parent != null && callbacks.isInstance(parent)) {
      return (T) parent;

    } else {
      FragmentActivity activity = frag.getActivity();
      if (activity != null && callbacks.isInstance(activity)) {
        return (T) activity;
      }
    }

    // We haven't actually failed a class cast thanks to the checks above, but that's the
    // idiomatic approach for this pattern with fragments.
    throw new ClassCastException(
        "This fragment's activity or parent fragment must implement "
            + callbacks.getCanonicalName());
  }

  public static Uri buildRawResourceUri(Context ctx, String filename) {
    // strip off any file extension from the video, because Android.
    return Uri.parse(
        String.format(
            "android.resource://%s/raw/%s", ctx.getPackageName(), removeExtension(filename)));
  }

  /**
   * For when you don't care if the Closeable you're closing is null, and you don't care that
   * closing a non-null buffer threw an exception, but you still care enough that logging might be
   * useful, like in a "finally" block, after you've already returned to the caller.
   *
   * @param closeable
   */
  public static void closeThisThingOrMaybeDont(Closeable closeable) {
    if (closeable == null) {
      log.d("Can't close closable, arg was null.");
      return;
    }

    try {
      closeable.close();
    } catch (IOException e) {
      log.d("Couldn't close closable, but that's apparently OK.  Error was: " + e.getMessage());
    }
  }

  private static String removeExtension(final String filename) {
    if (filename == null) {
      return null;
    }
    int indexOfExtension = filename.lastIndexOf(".");
    if (indexOfExtension == -1) {
      return filename;
    } else {
      return filename.substring(0, indexOfExtension);
    }
  }
}