Android Open Source - twawm2 Android Utils






From Project

Back to project page twawm2.

License

The source code is released under:

Copyright (c) 2014, afnf All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistr...

If you think the Android project twawm2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.appspot.afnf4199ga.utils;
//from ww w.ja  v a2  s .c o m
import java.util.Locale;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.wifi.WifiManager;

import com.appspot.afnf4199ga.twawm.Const;

public class AndroidUtils {

  private AndroidUtils() {

  }

  public static String getAction(Intent intent) {
    if (intent != null) {
      return intent.getAction();
    }
    return null;
  }

  public static String getActionForLog(Intent intent) {
    String action = getAction(intent);
    if (action != null) {
      if (action.startsWith(Const.INTENT_PREFIX)) {
        return action.substring(Const.INTENT_PREFIX.length());
      }
      else {
        return action;
      }
    }
    return null;
  }

  public static boolean isActionEquals(Intent intent, String expected) {
    if (expected == null) {
      return false;
    }
    else {
      String action = getAction(intent);
      return action != null && action.equals(expected);
    }
  }

  public static boolean isUIThread(Context context) {
    return Thread.currentThread().equals(context.getMainLooper().getThread());
  }

  public static int indexOf(String[] datas, String val) {
    if (val != null) {
      for (int i = 0; i < datas.length; i++) {
        if (datas[i].equals(val)) {
          return i;
        }
      }
    }
    return -1;
  }

  public static boolean isWifiDisabled(WifiManager wifi) {
    return wifi.getWifiState() == WifiManager.WIFI_STATE_DISABLED;
  }

  public static boolean isWifiEnabled(WifiManager wifi) {
    return wifi.getWifiState() == WifiManager.WIFI_STATE_ENABLED;
  }

  public static boolean isWifiEnabledOrEnabling(WifiManager wifi) {
    int wifiState = wifi.getWifiState();
    return wifiState == WifiManager.WIFI_STATE_ENABLED || wifiState == WifiManager.WIFI_STATE_ENABLING;
  }

  public static void sleep(long ms) {
    try {
      Thread.sleep(ms);
    }
    catch (InterruptedException e) {
    }
  }

  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // ???
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  public static int getIntFromEditPref(SharedPreferences pref, String key, String default_value) {
    String value = pref.getString(key, default_value);
    if (MyStringUtlis.isEmpty(value)) {
      value = default_value;
    }
    return Integer.parseInt(value);
  }

  public static String getPrefString(SharedPreferences pref, String key) {
    Object value = getPrefObject(pref, key);
    if (value instanceof String) {
      String s = (String) value;
      if (MyStringUtlis.isEmpty(s) == false) {
        return s;
      }
    }
    return null;
  }

  public static Boolean getPrefBoolean(SharedPreferences pref, String key) {
    Object value = getPrefObject(pref, key);
    return value != null ? (Boolean) value : null;
  }

  protected static Object getPrefObject(SharedPreferences pref, String key) {

    // ????????????
    Map<String, ?> all = pref.getAll();
    if (all != null) {
      Object obj = all.get(key);
      return obj;
    }

    return null;
  }

  public static Object updatePrefValueByModel(SharedPreferences pref, String key, String thisModel, String targetModel,
      Object targetModelValue, Object notTargetModelValue) {

    // ????????????????????
    Object value;
    String mdl = "#" + thisModel.toUpperCase(Locale.US) + "#";
    if (targetModel != null && targetModel.indexOf(mdl) != -1) {
      value = targetModelValue;
    }
    else {
      value = notTargetModelValue;
    }

    if (value != null) {
      Editor edit = pref.edit();
      if (value instanceof String) {
        edit.putString(key, (String) value);
      }
      else if (value instanceof Boolean) {
        edit.putBoolean(key, (Boolean) value);
      }
      edit.commit();
    }

    return value;
  }
}




Java Source Code List

com.appspot.afnf4199ga.twawm.BluetoothHelper.java
com.appspot.afnf4199ga.twawm.Const.java
com.appspot.afnf4199ga.twawm.Const.java
com.appspot.afnf4199ga.twawm.HostnameListTest.java
com.appspot.afnf4199ga.twawm.HostnameList.java
com.appspot.afnf4199ga.twawm.IconSelectorTest.java
com.appspot.afnf4199ga.twawm.IconSelector.java
com.appspot.afnf4199ga.twawm.OnlineChecker.java
com.appspot.afnf4199ga.twawm.StateMachineTest.java
com.appspot.afnf4199ga.twawm.StateMachine.java
com.appspot.afnf4199ga.twawm.TwawmUtils.java
com.appspot.afnf4199ga.twawm.app.BackgroundServiceTest.java
com.appspot.afnf4199ga.twawm.app.BackgroundService.java
com.appspot.afnf4199ga.twawm.app.DefaultWidgetProvider.java
com.appspot.afnf4199ga.twawm.app.InfoActivity.java
com.appspot.afnf4199ga.twawm.app.InitialConfigurationWizardActivity.java
com.appspot.afnf4199ga.twawm.app.LogSendActivity.java
com.appspot.afnf4199ga.twawm.app.MainActivity.java
com.appspot.afnf4199ga.twawm.app.MainApp.java
com.appspot.afnf4199ga.twawm.app.MyPreferenceActivity.java
com.appspot.afnf4199ga.twawm.app.NetworkSwitcher.java
com.appspot.afnf4199ga.twawm.app.StaticIntentListener.java
com.appspot.afnf4199ga.twawm.app.UIAct.java
com.appspot.afnf4199ga.twawm.ctl.CustomizeActionsActivityTest.java
com.appspot.afnf4199ga.twawm.ctl.CustomizeActionsActivity.java
com.appspot.afnf4199ga.twawm.ctl.CwacTouchListView.java
com.appspot.afnf4199ga.twawm.ctl.ListItem.java
com.appspot.afnf4199ga.twawm.router.EcoModeControlTest.java
com.appspot.afnf4199ga.twawm.router.EcoModeControl.java
com.appspot.afnf4199ga.twawm.router.InetLookupWrappter.java
com.appspot.afnf4199ga.twawm.router.InetLookupWrappter.java
com.appspot.afnf4199ga.twawm.router.MyHttpClientTest.java
com.appspot.afnf4199ga.twawm.router.MyHttpClient.java
com.appspot.afnf4199ga.twawm.router.MyHttpClient.java
com.appspot.afnf4199ga.twawm.router.RouterControlByHttpTest.java
com.appspot.afnf4199ga.twawm.router.RouterControlByHttp.java
com.appspot.afnf4199ga.twawm.router.RouterControlByHttp.java
com.appspot.afnf4199ga.twawm.router.RouterControl.java
com.appspot.afnf4199ga.twawm.router.RouterControl.java
com.appspot.afnf4199ga.twawm.router.RouterInfo.java
com.appspot.afnf4199ga.twawm.router.RouterInfo.java
com.appspot.afnf4199ga.utils.AndroidUtilsTest.java
com.appspot.afnf4199ga.utils.AndroidUtils.java
com.appspot.afnf4199ga.utils.AndroidUtils.java
com.appspot.afnf4199ga.utils.Logger.java
com.appspot.afnf4199ga.utils.Logger.java
com.appspot.afnf4199ga.utils.MyStringUtlisTest.java
com.appspot.afnf4199ga.utils.MyStringUtlis.java
com.appspot.afnf4199ga.utils.MyStringUtlis.java
com.appspot.afnf4199ga.utils.MyTestUtils.java
com.appspot.afnf4199ga.utils.MyUncaughtExceptionHandler.java
com.appspot.afnf4199ga.utils.MyUncaughtExceptionHandler.java
com.appspot.afnf4199ga.wmgraph.app.FetchThread.java
com.appspot.afnf4199ga.wmgraph.app.InetLookupThread.java
com.appspot.afnf4199ga.wmgraph.app.InfoActivity.java
com.appspot.afnf4199ga.wmgraph.app.MainActivity.java
com.appspot.afnf4199ga.wmgraph.app.MyPreferenceActivity.java
com.appspot.afnf4199ga.wmgraph.app.UIAct.java
net.afnf.and.twawm2.DexmakerInstrumentationTestCase.java
net.afnf.and.twawm2.MyInstrumentationTestRunner.java