[Q] Same shell command works from console and doesn't work from Activity

Search This thread

Torsionick

Member
Feb 11, 2012
25
5
Yalta
//sorry for my english, if any

Hi. I'm using AIDE right on my device (Xperia NeoV, 4.1.B.0.587, root)

And i'm trying to inject key events through the same shell command:
"input keyevent 25" via "su", both in AIDE Console and separated Activity.

//volume-down key is not my target, just good for tests. I've tried different keys, same result.

Part of the code:
Code:
try {
   java.lang.Process p = Runtime.getRuntime().exec("su");
   DataOutputStream dos = new DataOutputStream(p.getOutputStream());
   dos.writeBytes("input keyevent 25\n");
   dos.flush();
} catch (IOException ex) {
   //log any errors
}

So, when it goes in AIDE ConsoleApp - it pushes down volume.
But when it executes from my Activity - nothing happens (no error messages in log, dos != null);

Maybe there should be some specific permission on manifest?
"android.permission.ACCESS_SUPERUSER" - no changes.

Maybe there should be some trick in code? Or(and?) i'm very stupid. Also, maybe somewhere a whole listing of _simple_ keyInjection project exists?
 
Last edited:

Masrepus

Senior Member
Feb 12, 2013
767
99
I managed to solve it already. I'm using this class now:
public void RunAsRoot(String[] cmds){
Process p = null;
try {
p = Runtime.getRuntime().exec("su");
} catch (IOException e) {
e.printStackTrace();
}
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : cmds) {
try {
os.writeBytes(tmpCmd+"\n");
} catch (IOException e) {
e.printStackTrace();
}
}
try {
os.writeBytes("exit\n");
} catch (IOException e) {
e.printStackTrace();
}
try {
os.flush();
} catch (IOException e) {
e.printStackTrace();
}

Then simply call it via
String[] commands = {"command1", "command2", "command3", ...};
RunAsRoot(commands);

Thanks anyways!

Credits: @KrauseDroid
Our original thread i took it from:
http://xdaforums.com/showthread.php?t=2725173
---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
 

Torsionick

Member
Feb 11, 2012
25
5
Yalta

I saw that thread before creating own, and tried solutions from it. They works same way - only from ConsoleApp in AIDE.

Also, last method is the same as I'm already using, but I've tried to copy code in project - no progress.

In addition:
- superuser rights granted to both.
- test "ls" command put into dos object gives me same list of current dir in both projects, so commands seems to run.
 

Torsionick

Member
Feb 11, 2012
25
5
Yalta
Listing:


MainActivity:
Code:
 package com.tsk.mk;

import android.app.*;
import android.content.*;
import android.os.*;
import android.widget.*;
import java.io.*;

public class MainActivity extends Activity {
	
	public static TextView logView;
	
 [user=439709]@override[/user]
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		logView = (TextView) findViewById(R.id.log);
		
		log("creating sercice");
		final MainActivity me = this;
		new Thread() {
 [user=439709]@override[/user]
			public void run() {
				log("thread.run");
				startService(new Intent(me, MissedKeysService.class));
				log("thread: service shoul'd be created");
			}
		}.start();
		log("end of MA.onCreate method");
	}

	static void log(String message) {
		logView.setText(logView.getText() + "\n" + message);
	}
	
}

MissedKeysService:
Code:
 package com.tsk.mk;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import java.io.*;

public class MissedKeysService extends Service {
	
	private WindowManager windowManager;
	private MissedKeysView mkv;
	private WindowManager.LayoutParams params;
	private DataOutputStream dos;

 [user=439709]@override[/user]
	public IBinder onBind(Intent intent) {
		MainActivity.log("onBind called o_o");
		return null;
	}
	
 [user=439709]@override[/user]
	public void onCreate() {
		MainActivity.log("Service.onCreate");
		super.onCreate();
		
		try {
			java.lang.Process p = Runtime.getRuntime().exec("su");
			dos = new DataOutputStream(p.getOutputStream());
		} catch (IOException ex) {
			MainActivity.log(ex.getMessage());
			dos = null;
		}

		windowManager = (WindowManager)
			getSystemService(WINDOW_SERVICE);
		mkv = new MissedKeysView(this, this);
		params = new WindowManager.LayoutParams(
			WindowManager.LayoutParams.WRAP_CONTENT,
			WindowManager.LayoutParams.WRAP_CONTENT,
			WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
			WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
			PixelFormat.TRANSLUCENT);
		params.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
		params.width = MissedKeysView.keySize;
		params.height = MissedKeysView.keySize;
		windowManager.addView(mkv, params);
		MainActivity.log("Service started");
	}
	
 [user=439709]@override[/user]
	public void onDestroy () {
		super.onDestroy ();
		if (mkv != null) windowManager.removeView(mkv);
		MainActivity.log("Ssrvice is ended");
	}
	
	public void extend(boolean state) {
		params.height = mkv.keySize * (state ? MissedKeysView.keys : 1);
		windowManager.updateViewLayout(mkv, params);
	}
	
	public void sendKey(int code) {
		if (dos == null) MainActivity.log("dos is null");
		try {
			dos.writeBytes("input keyevent " + code + "\n");
			dos.flush();
			MainActivity.log("" + code);
		} catch (IOException e) {
			MainActivity.log("wtf?");
		}
	}

}

MissedKeysView:
Code:
 package com.tsk.mk;

import android.content.*;
import android.graphics.*;
import android.view.*;
import android.view.View.*;
import java.io.*;

public class MissedKeysView extends View
implements OnTouchListener {
	
	final static int keySize = 64;
	final static String[] labels = {":", "+", "-", "^", "v", "o", "<", ">"};
	final private int[] codes = {-1, 24, 25, 19, 20, 23, 21, 22};
	final static int keys = 3; //max shown keys
	
	MissedKeysService mks;
	Paint bgP; //background
	Paint hbgP; //highlighted bg
	Paint tP; //text
	int selected = -1; //active key
	
	public MissedKeysView(Context context, MissedKeysService mks) {
		super(context);
		this.mks = mks;
		bgP = new Paint();
		bgP.setARGB(64, 128, 128, 128);
		hbgP = new Paint();
		hbgP.setARGB(64, 255, 128, 0);
		tP = new Paint();
		tP.setARGB(128, 255, 255, 255);
		tP.setTextSize(keySize);
		tP.setTextAlign(Paint.Align.CENTER);
		setOnTouchListener(this);
	}

 [user=439709]@override[/user]
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		for(int i=0; i<getHeight()/keySize; i++) {
			canvas.drawCircle(keySize/2,
				keySize/2 + i*keySize, keySize/2,
				(i == selected ? hbgP : bgP));
			canvas.drawText(labels[i], keySize/2, i*keySize + keySize*3/4, tP);
		}
	}

 [user=439709]@override[/user]
	public boolean onTouch(View v, MotionEvent event) {
		switch(event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				selected = (int) (event.getY()/keySize);
				if (selected == 0) mks.extend((int) getHeight() <= keySize);
				else mks.sendKey(codes[selected]);
				break;
			case MotionEvent.ACTION_UP:
				selected =-1;
		}
		invalidate();
		return super.onTouchEvent(event);
	}
	
}

AndroidManifest:
Code:
 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tsk.mk"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk 
        android:minSdkVersion="8" 
        android:targetSdkVersion="11" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
		
		<service
			android:name=".MissedKeysService"
			android:exported="true" >
		</service>
    </application>
	
	<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
	<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
    <uses-permission android:name="android.permission.INJECT_EVENTS" />

</manifest>

 
Last edited:

Masrepus

Senior Member
Feb 12, 2013
767
99
@Torsionick do you have the permission INJECT_EVENTS

---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk