|
public class SDcardService extends Service { |
|
@Override |
|
public IBinder onBind(Intent intent) { |
|
// Return the communication channel to the service. |
|
throw new UnsupportedOperationException("Not yet implemented"); |
|
} |
|
|
|
@Override |
|
public void onCreate() { |
|
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED); |
|
intentFilter.addAction (Intent.ACTION_MEDIA_UNMOUNTED); |
|
intentFilter.addAction (Intent.ACTION_MEDIA_REMOVED); |
|
intentFilter.addAction (Intent.ACTION_MEDIA_BAD_REMOVAL); |
|
intentFilter.addDataScheme ("file"); |
|
registerReceiver (broadcastRec, intentFilter); |
|
|
|
Toast.makeText(this, "SDcard Plug/Unplug Service was Created", Toast.LENGTH_LONG).show(); |
|
} |
|
|
|
@Override |
|
public void onStart(Intent intent, int startId) { |
|
// For time consuming an long tasks you can launch a new thread here... |
|
Toast.makeText(this, "SDcard Plug/Unplug Service Started", Toast.LENGTH_LONG).show(); |
|
} |
|
|
|
@Override |
|
public void onDestroy() { |
|
Toast.makeText(this, "SDcard Plug/Unplug Service Destroyed", Toast.LENGTH_LONG).show(); |
|
} |
|
|
|
private final BroadcastReceiver broadcastRec = new BroadcastReceiver (){ |
|
public void onReceive(Context context, Intent intent) { |
|
String path_extsd = getExternalSDCardPath(); |
|
String filename_autorun_plug = "autorun_plug.sh"; |
|
String filename_autorun_unplug = "autorun_unplug.sh"; |
|
String path_autorun_plug = path_extsd + "/" + filename_autorun_plug; |
|
String path_autorun_unplug = path_extsd + "/" + filename_autorun_unplug; |
|
|
|
File file_autorun_plug = new File(path_autorun_plug); |
|
File file_autorun_unplug = new File(path_autorun_unplug); |
|
|
|
if (intent.getAction().equals(Intent.ACTION_MEDIA_REMOVED)) { |
|
dispmsg("ACTION_MEDIA_REMOVED"); |
|
Utils.rootExecCmdRetExitValue(filename_autorun_unplug); |
|
|
|
} else if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){ |
|
dispmsg("ACTION_MEDIA_MOUNTED: " + path_extsd); |
|
|
|
if(!file_autorun_unplug.exists()){ |
|
dispmsg(String.format("%s not exist.", path_autorun_unplug)); |
|
} else { |
|
Utils.rootExecCmdRetExitValue("mount -o remount rw /system"); |
|
Utils.rootExecCmdRetExitValue(String.format("busybox cp %s /system/xbin/", path_autorun_unplug)); |
|
Utils.rootExecCmdRetExitValue(String.format("chmod 777 /system/xbin/%s", filename_autorun_unplug)); |
|
} |
|
|
|
if(!file_autorun_plug.exists()){ |
|
dispmsg(String.format("%s not exist.", path_autorun_plug)); |
|
} else { |
|
Utils.rootExecCmdRetExitValue("mount -o remount rw /system"); |
|
Utils.rootExecCmdRetExitValue(String.format("busybox cp %s /system/xbin/", path_autorun_plug)); |
|
Utils.rootExecCmdRetExitValue(String.format("chmod 777 /system/xbin/%s", filename_autorun_plug)); |
|
Utils.rootExecCmdRetExitValue(filename_autorun_plug); |
|
} |
|
} |
|
} |
|
}; |
|
|
|
private void dispmsg(String msg){ |
|
Toast.makeText(mitac.autorunSD.SDcardService.this, msg, Toast.LENGTH_LONG).show(); |
|
} |
|
|
|
public static String getExternalSDCardPath() { |
|
if (Utils.execCmdRetOut("cat /proc/mounts").contains("external_sd")){ |
|
return "/mnt/external_sd"; |
|
} else { |
|
// TODO the external sdcard path? |
|
} |
|
//Environment.getExternalStorageDirectory().getPath() always return /mnt/sdcard |
|
return null; |
|
} |
|
} |