Need to loop through /dev/block/platform on rooted device - Java for Android App Development

Hi,
I'm updating an application I have that switches the recovery partition on a device. Currently, I have various recovery paths for various devices hard coded. What I'd like to do it loop through /dev/block/platform directory and read the subdirectories to find the by-name directory and then recovery partition. This was my app can support more devices that the ones I have hard coded. I can't figure out for the life of me how to read this directory. Can someone show me an example of how I'd go about doing this? I've tried the standard Java way like this But that didn't work:
Code:
protected void onCreate(Bundle savedInstanceState) {
File[] files = new File("/dev/block/platform").listFiles();
showFiles(files);
}
public static void showFiles(File[] files) {
for (File file : files) {
if (file.isDirectory()) {
Log.d("TEST","Directory: " + file.getName());
showFiles(file.listFiles()); // Calls same method again.
} else {
Log.d("TEST", "File: " + file.getName());
}
}
}
Thanks in advance

Related

[Q] Problem to run the Torch from source

Hello, i have downloaded the Torch source code from cyanogen repository and i have this problem:
1º I put the code in a empty project in Eclipse
2º I havent errors and the code compile correctly
3º I run the app in my device (Nexus One) and work well all buttons and options, but when i press in "On" button for active i get a error and the app crash.
Debugging i have seen the following:
For active flash i have put a "1" in file "/sys/devices/platform/flashlight.0/leds/flashlight/brightness" (If i put a "1" manually with terminal the flash is active correctly)
In code, we have this:
public class FlashDevice {
private static final String DEVICE = "/sys/devices/platform/flashlight.0/leds/flashlight/brightness";
...
public synchronized void setFlashMode(int mode) {
try {
if (mWriter == null) {
mWriter = new FileWriter(DEVICE);
}
int value = mode;
switch (mode) {
case STROBE:
value = OFF;
break;
case DEATH_RAY:
value = useDeathRay ? DEATH_RAY : HIGH;
break;
}
mWriter.write(String.valueOf(value));
mWriter.flush();
mFlashMode = mode;
if (mode == OFF) {
mWriter.close();
mWriter = null;
}
} catch (IOException e) {
throw new RuntimeException("Can't open flash device: " + DEVICE, e);
}
}
public synchronized int getFlashMode() {
return mFlashMode;
}
}
Click to expand...
Click to collapse
I get this error in line "mWriter = new FileWriter(DEVICE);":
java.io.FileNotFoundException: /sys/devices/platform/flashlight.0/leds/flashlight/brightness (Permission denied)
Someone know the cause????
Try using the original source code from the repository by
Code:
svn checkout http://n1torch.googlecode.com/svn/trunk/ n1torch-read-only
and importing it as a project into eclipse.

[Q] Writing to your Apps data folder

im trying to write a Serializable Object to the data folder of my app. I was using this code to specify the path to the file and write the Serializable Object
Code:
/**
* writeMissedCalls()
*
* @param context - the Context of the application
* @return if the missedCalls were written to file successfully
*/
public static boolean writeMissedCalls(Context context) {
String filename = context.getApplicationInfo().dataDir + "/" + MCWUtils.MCW_DATA_FILE;
FileOutputStream fos;
ObjectOutputStream out;
try {
fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
out = new ObjectOutputStream(fos);
out.writeObject(MissedCallWidget.missedCalls);
out.close(); }
catch (FileNotFoundException e) { return false; }
catch (IOException e) { return false; }
return true;
}
when i try to write this Object in the onDisabled() of my AppWidgetProvider class i get an error of this sort
java.lang.RuntimeException: Unable to start receiver com.tsb.fistfulofneurons.missedcallwidget.MissedCallWidget: java.lang.IllegalArgumentException: File /data/data/com.tsb.fistfulofneurons.missedcallwidget/missed_calls.dat contains a path separator
do i not need to specify the path to my apps data folder? will the openFileOutput() specify the path to the data for me?
so instead of passing the path "/data/data/com.tsb.fistfulofneurons.missedcallwidget/missed_calls.dat" just pass "missed_calls.dat"?
thanks!
I've not tried to open a file in this manner, but I would guess that it defaults to the apps data directory. Why not give it a try and see?
Gene Poole said:
I've not tried to open a file in this manner, but I would guess that it defaults to the apps data directory. Why not give it a try and see?
Click to expand...
Click to collapse
yea it appears to default to the /data/data folder for your package. the documentation appears to be lacking. thanks

stranges with android mediaservice

I've got nexus 4 and have some issue with media servicem cause my MS could not find music on my internal SD card; So I make an app;
Code:
public class SingleMediaScanner implements MediaScannerConnectionClient {
private MediaScannerConnection mMs;
private File mFile;
public SingleMediaScanner(Context context, File f) {
mFile = f;
mMs = new MediaScannerConnection(context, this);
mMs.connect();
}
[user=439709]@override[/user]
public void onMediaScannerConnected() {
Log.v("MS", "connected");
mMs.scanFile(mFile.getAbsolutePath(), "audio/*");
Log.v("MS-file", mFile.getAbsolutePath());
}
[user=439709]@override[/user]
public void onScanCompleted(String path, Uri uri) {
mMs.disconnect();
}
}
Code:
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath()+"/Music/");
for (int i=0;i<dir.listFiles().length;i++)
{
if (dir.listFiles()[i].isFile()==true&&dir.listFiles()[i].getName().endsWith(".mp3"))
{
new SingleMediaScanner(this, dir.listFiles()[i].getAbsoluteFile());
}
Log says that MS was connected and tried to scan media file, but file doesnt appear in Media library , what's the problem? by the way I have CM ROM

Dynamically loading an external library at runtime

I am using and android 2.2 phone for testing and getting an error which is driving me crazy
07-27 01:24:55.692: W/System.err(14319): java.lang.ClassNotFoundException: com.shoaib.AndroidCCL.MyClass in loader [email protected]
Can someone help me what to do now..i have tried various suggested solutions on different posts
First I wrote the following class:
Code:
package org.shoaib.androidccl;
import android.util.Log;
public class MyClass {
public MyClass() {
Log.d(MyClass.class.getName(), "MyClass: constructor called.");
}
public void doSomething() {
Log.d(MyClass.class.getName(), "MyClass: doSomething() called.");
}
}
And I packaged it in a DEX file that I saved on my device's SD card as `/sdcard/testdex.jar`.
Then I wrote the program below, after having removed `MyClass` from my Eclipse project and cleaned it:
Code:
public class Main extends Activity {
[user=1299008]@supp[/user]ressWarnings("unchecked")
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
final String libPath = Environment.getExternalStorageDirectory() + "/testdex.jar";
final File tmpDir = getDir("dex", 0);
final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, this.getClass().getClassLoader());
final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("org.shoaib.androidccl.MyClass");
final Object myInstance = classToLoad.newInstance();
final Method doSomething = classToLoad.getMethod("doSomething");
doSomething.invoke(myInstance);
} catch (Exception e) {
e.printStackTrace();
}
}
}
As far as I know you can't load external dex classes (and certainly not a jar file), the only ones that can be loaded are the ones packaged in the classes.dex file inside the APK and protected by its signature and certificate.
This would be quite a security flaw if your app could be "clean" at install but then download malicious classes and execute them.

How to record the Android Touch event?

I have to record the touch event of any android (.apk) application. For that I have created the .jar(Library) file which is responsible to record touch event when user press any control. This .jar will be injected with any .apk file.
For achieve the above things I have follow the below step.
1)I have created application with only one button name App.apk.
2)Also Create the another library project called Test.jar
3)Give the dependency of Test.jar to App.apk.
Now I want send same event to test.jar. I have write below code in App.apk activity
@override
public void onResume()
{
super.onResume();
Test.Event.MyHook(this);
}
@override
public void onPause()
{
super.onPause();
Test.Event.MyUnHook(this);
}
@override
public boolean dispatchKeyEvent(android.view.KeyEvent event)
{
Test.Event.MyEvent(event);
return super.dispatchKeyEvent(event);
}
Can u suggest the way to get button click event from App.apk to Test.jar ?
Thanks
maheng said:
I have to record the touch event of any android (.apk) application. For that I have created the .jar(Library) file which is responsible to record touch event when user press any control. This .jar will be injected with any .apk file.
For achieve the above things I have follow the below step.
1)I have created application with only one button name App.apk.
2)Also Create the another library project called Test.jar
3)Give the dependency of Test.jar to App.apk.
Now I want send same event to test.jar. I have write below code in App.apk activity
@override
public void onResume()
{
super.onResume();
Test.Event.MyHook(this);
}
@override
public void onPause()
{
super.onPause();
Test.Event.MyUnHook(this);
}
@override
public boolean dispatchKeyEvent(android.view.KeyEvent event)
{
Test.Event.MyEvent(event);
return super.dispatchKeyEvent(event);
}
Can u suggest the way to get button click event from App.apk to Test.jar ?
Thanks
Click to expand...
Click to collapse
Seems rather fishy why you'd have to do that, but let's roll with it...^^
TLDR: in pure java, using standard APIs or even reflection or root, you can't.
You could use the android-event-injector project (which is a C++/JNI library) : https://code.google.com/p/android-event-injector/
It provides methods to listen to all input events, something like that (this will send all the results to logcat):
PHP:
public void StartEventMonitor() {
m_bMonitorOn = true;
Thread b = new Thread(new Runnable() {
public void run() {
while (m_bMonitorOn) {
for (InputDevice idev:events.m_Devs) {
// Open more devices to see their messages
if (idev.getOpen() && (0 == idev.getPollingEvent())) {
final String line = idev.getName()+
":" + idev.getSuccessfulPollingType()+
" " + idev.getSuccessfulPollingCode() +
" " + idev.getSuccessfulPollingValue();
Log.d(LT, "Event:"+line);
}
}
}
}
});
b.start();
}
You can read more here: http://www.pocketmagic.net/2013/01/programmatically-injecting-events-on-android-part-2/
However, you might also be able to do that using the Xposed framework and finding a hook into the actual framework class for dispatchKeyEvent if it's the event you want to intercept.
I've never with Xposed so I can't really help you with the implementation, though, but it might be something to look into.

Categories

Resources