Created
April 16, 2014 20:49
-
-
Save k4zy/10931581 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.co.denso.android.denmobi.others; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.net.InetAddress; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Scanner; | |
import android.app.AlertDialog; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.DialogInterface.OnClickListener; | |
import android.net.wifi.WifiConfiguration; | |
import android.os.Environment; | |
import android.util.Log; | |
import android.view.Window; | |
import android.view.WindowManager; | |
/** | |
* Utility用のクラス | |
* | |
*/ | |
public class UtiltyClass { | |
public final static String AppDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DrivingRecorder/"; | |
public final static String sdDirectoryPath = "/ext_card/DrivingRecorder/"; | |
public final static String TAG = "DriveRecorder"; | |
public static enum ScanType { heart, cycle,speed; }; | |
public static void showDialog(Context context, String title,String message){ | |
OnClickListener positiveLisner = new OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
} | |
}; | |
OnClickListener negativeLisner = new OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
} | |
}; | |
showDialog(context, title, message, positiveLisner, negativeLisner); | |
} | |
public static void showDialog(Context context, String title,String message,OnClickListener positiveLisner){ | |
OnClickListener negativeLisner = new OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
} | |
}; | |
showDialog(context, title, message, positiveLisner, negativeLisner); | |
} | |
public static void showDialog(Context context, String title,String message,OnClickListener positiveLisner,OnClickListener negativeLisner){ | |
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); | |
alertDialogBuilder.setTitle(title); | |
alertDialogBuilder.setMessage(message); | |
alertDialogBuilder.setPositiveButton("はい",positiveLisner); | |
alertDialogBuilder.setNegativeButton("いいえ",negativeLisner); | |
alertDialogBuilder.setCancelable(false); | |
AlertDialog alertDialog = alertDialogBuilder.create(); | |
alertDialog.show(); | |
} | |
public static void addLegacyOverflowButton(Window window) { | |
if (window.peekDecorView() == null) { | |
throw new RuntimeException("Must call addLegacyOverflowButton() after setContentView()"); | |
} | |
try { | |
window.addFlags(WindowManager.LayoutParams.class.getField("FLAG_NEEDS_MENU_KEY").getInt(null)); | |
} | |
catch (NoSuchFieldException e) { | |
// Ignore since this field won't exist in most versions of Android | |
} | |
catch (IllegalAccessException e) { | |
Log.w(TAG, "Could not access FLAG_NEEDS_MENU_KEY in addLegacyOverflowButton()", e); | |
} | |
} | |
public static void setIpAssignment(String assign , WifiConfiguration wifiConf) | |
throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException{ | |
setEnumField(wifiConf, assign, "ipAssignment"); | |
} | |
public static void setIpAddress(InetAddress addr, int prefixLength, WifiConfiguration wifiConf) | |
throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, | |
NoSuchMethodException, ClassNotFoundException, InstantiationException, InvocationTargetException{ | |
Object linkProperties = getField(wifiConf, "linkProperties"); | |
if(linkProperties == null)return; | |
Class laClass = Class.forName("android.net.LinkAddress"); | |
Constructor laConstructor = laClass.getConstructor(new Class[]{InetAddress.class, int.class}); | |
Object linkAddress = laConstructor.newInstance(addr, prefixLength); | |
ArrayList mLinkAddresses = (ArrayList)getDeclaredField(linkProperties, "mLinkAddresses"); | |
mLinkAddresses.clear(); | |
mLinkAddresses.add(linkAddress); | |
} | |
public static void setGateway(InetAddress gateway, WifiConfiguration wifiConf) | |
throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, | |
ClassNotFoundException, NoSuchMethodException, InstantiationException, InvocationTargetException{ | |
Object linkProperties = getField(wifiConf, "linkProperties"); | |
if(linkProperties == null)return; | |
Class routeInfoClass = Class.forName("android.net.RouteInfo"); | |
Constructor routeInfoConstructor = routeInfoClass.getConstructor(new Class[]{InetAddress.class}); | |
Object routeInfo = routeInfoConstructor.newInstance(gateway); | |
ArrayList mRoutes = (ArrayList)getDeclaredField(linkProperties, "mRoutes"); | |
mRoutes.clear(); | |
mRoutes.add(routeInfo); | |
} | |
public static void setDNS(InetAddress dns, WifiConfiguration wifiConf) | |
throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException{ | |
Object linkProperties = getField(wifiConf, "linkProperties"); | |
if(linkProperties == null)return; | |
ArrayList<InetAddress> mDnses = (ArrayList<InetAddress>)getDeclaredField(linkProperties, "mDnses"); | |
mDnses.clear(); //or add a new dns address , here I just want to replace DNS1 | |
mDnses.add(dns); | |
} | |
public static Object getField(Object obj, String name) | |
throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{ | |
Field f = obj.getClass().getField(name); | |
Object out = f.get(obj); | |
return out; | |
} | |
public static Object getDeclaredField(Object obj, String name) | |
throws SecurityException, NoSuchFieldException, | |
IllegalArgumentException, IllegalAccessException { | |
Field f = obj.getClass().getDeclaredField(name); | |
f.setAccessible(true); | |
Object out = f.get(obj); | |
return out; | |
} | |
public static void setEnumField(Object obj, String value, String name) | |
throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{ | |
Field f = obj.getClass().getField(name); | |
f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value)); | |
} | |
/** | |
* 外部SDカードのパスを取得する | |
* | |
* @return external SD card path, or null | |
*/ | |
public static String getExternalSdCardPath(){ | |
HashSet<String> paths = new HashSet<String>(); | |
Scanner scanner = null; | |
try{ | |
// システム設定ファイルを読み込み | |
File vold_fstab = new File("/system/etc/vold.fstab"); | |
scanner = new Scanner(new FileInputStream(vold_fstab)); | |
while(scanner.hasNextLine()){ | |
String line = scanner.nextLine(); | |
// dev_mountまたはfuse_mountで始まる行 | |
if(line.startsWith("dev_mount") || line.startsWith("fuse_mount")){ | |
// 半角スペースではなくタブで区切られている機種もあるらしい | |
// 半角スペース区切り3つめ(path)を取得 | |
String path = line.replaceAll("\t", " ").split(" ")[2]; | |
paths.add(path); | |
} | |
} | |
}catch(FileNotFoundException e){ | |
e.printStackTrace(); | |
return null; | |
}finally{ | |
if(scanner != null){ | |
scanner.close(); | |
} | |
} | |
// Environment.getExternalStorageDirectory() が内部SDを返す場合は除外 | |
if(!Environment.isExternalStorageRemovable()){ | |
paths.remove(Environment.getExternalStorageDirectory().getPath()); | |
} | |
// マウントされているSDカードのパスを追加 | |
List<String> mountSdCardPaths = new ArrayList<String>(); | |
for(String path : paths){ | |
if(isMounted(path)){ | |
mountSdCardPaths.add(path); | |
} | |
} | |
// マウントされているSDカードのパス | |
String mountSdCardPath = null; | |
if(mountSdCardPaths.size() > 0){ | |
mountSdCardPath = mountSdCardPaths.get(0); | |
} | |
if(mountSdCardPath!=null){ | |
mountSdCardPath = mountSdCardPath+"/DrivingRecorder/"; | |
} | |
return mountSdCardPath; | |
} | |
/** | |
* パスがマウントされているSDカードのパスかチェックする | |
* | |
* @param path SD card path | |
* @return true if path is the mounted SD card's path, otherwise false | |
*/ | |
public static boolean isMounted(String path){ | |
boolean isMounted = false; | |
Scanner scanner = null; | |
try{ | |
// マウントポイントを取得する | |
File mounts = new File("/proc/mounts"); | |
scanner = new Scanner(new FileInputStream(mounts)); | |
while(scanner.hasNextLine()){ | |
if(scanner.nextLine().contains(path)){ | |
isMounted = true; | |
break; | |
} | |
} | |
}catch(FileNotFoundException e){ | |
e.printStackTrace(); | |
return false; | |
}finally{ | |
if(scanner != null){ | |
scanner.close(); | |
} | |
} | |
return isMounted; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment