Make Your Own Heat Sink for the LG Optimus 4X HD

Its not a rare occurrence that performing a resource heavy task on your Android device (e.g. … more

Learn How to Create an Old School Dialer

XDA is not only a great source for custom ROMs, kernels, and various modifications for numerous … more

USB Desktop Charger Roundup – XDA TV

Sometimes you learn one way to do something, and that’s the way you do it forever. You never … more

Samsung Galaxy Grand Duos Receives Early CM12 Port

Samsung Galaxy Grand is a dual-SIM phone with a 5 screen that was announced two years ago. … more

Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

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

OP Torsionick

6th May 2014, 02:26 PM   |  #1  
OP Junior Member
Flag Yalta
Thanks Meter: 0
 
8 posts
Join Date:Joined: Feb 2012
More
//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 by Torsionick; 6th May 2014 at 08:53 PM.
6th May 2014, 10:54 PM   |  #2  
Masrepus's Avatar
Senior Member
Thanks Meter: 79
 
702 posts
Join Date:Joined: Feb 2013
More
Quote:

I managed to solve it already. I'm using this class now:

Quote:

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
Quote:

String[] commands = {"command1", "command2", "command3", ...};
RunAsRoot(commands);

Thanks anyways!

Credits: @KrauseDroid
Our original thread i took it from:
http://forum.xda-developers.com/show....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
7th May 2014, 08:32 AM   |  #3  
OP Junior Member
Flag Yalta
Thanks Meter: 0
 
8 posts
Join Date:Joined: Feb 2012
More
Quote:
Originally Posted by Masrepus

Credits: @KrauseDroid
Our original thread i took it from:
http://forum.xda-developers.com/show....php?t=2725173

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.
7th May 2014, 09:59 AM   |  #4  
OP Junior Member
Flag Yalta
Thanks Meter: 0
 
8 posts
Join Date:Joined: Feb 2012
More
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;
	
 @override
    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() {
 @override
			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;

 @override
	public IBinder onBind(Intent intent) {
		MainActivity.log("onBind called o_o");
		return null;
	}
	
 @override
	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");
	}
	
 @override
	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);
	}

 @override
	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);
		}
	}

 @override
	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 by Torsionick; 7th May 2014 at 10:05 AM.
7th May 2014, 09:25 PM   |  #5  
Masrepus's Avatar
Senior Member
Thanks Meter: 79
 
702 posts
Join Date:Joined: Feb 2013
More
@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
8th May 2014, 04:51 AM   |  #6  
OP Junior Member
Flag Yalta
Thanks Meter: 0
 
8 posts
Join Date:Joined: Feb 2012
More
Quote:
Originally Posted by Masrepus

@Torsionick do you have the permission INJECT_EVENTS

Thanks for participating in solving.

The reason was somewhere else - after executing "export LD_LIBRARY_PATH=/system/lib" all works fine. Thread is over.
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes