aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/hoopajoo/android/RemoteContext.java
blob: bf44cadeabc3718d865cbbac44cf117140260f9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package net.hoopajoo.android;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemClock;
import android.util.Log;
import android.view.IWindowManager;
import android.view.KeyEvent;
import android.os.IPowerManager;
import android.content.Context;

/*
 * Inject events as root in to the android framework, accepts commands on stdin,
 * 
 * This gives finer access control to keydown/up and also to put the device to sleep
 * 
 * Other things that are blocked from normal apps that have an interface could be added here
 * like pointer control
 */

public class RemoteContext {
    private boolean debug = false;
    private final String LOG = "RootContext";
    
    public static void main( String[] args ) {
        (new RemoteContext()).run( args );
    }

    private void run( String[] args ) {
        if( args.length > 0 ) {
            debug = true;
            d( "Debugging enabled" );
        }
        
        BufferedReader input = new BufferedReader(
                new InputStreamReader( System.in ),
                2 * 1024 );
        
        String cmd;
        try {
            while( true ) {
                cmd = input.readLine();
        
                d( "Read: " + cmd );
                
                String[] parts = cmd.split(  " " );

                if( debug ) {
                    for( String s : parts ) {
                        d( "Part: " + s );                    
                    }
                }
                
                boolean processed = false;
                if( parts.length > 0 ) {
                    if( parts[ 0 ].startsWith( "key") ) {
                        int code = Integer.parseInt( parts[ 1 ] );
                        if( parts[ 0 ].equals( "keycode" ) ) {
                            // key down, up
                            d( "Sending keycode: " + code );
                            sendKeyCode( code, KeyEvent.ACTION_DOWN );
                            sendKeyCode( code, KeyEvent.ACTION_UP );
                            processed = true;
                        }else if( parts[ 0 ].equals( "keycodedown" ) ) {
                            d( "Sending keycode DOWN: " + code );
                            sendKeyCode( code, KeyEvent.ACTION_DOWN );
                            processed = true;
                        }else if( parts[ 0 ].equals( "keycodeup" ) ) {
                            d( "Sending keycode UP: " + code );
                            sendKeyCode( code, KeyEvent.ACTION_UP );
                            processed = true;
                        }
                    }
                    
                    if( parts[ 0 ].equals( "sleep" ) ) {
                        d( "Going to sleep" );
                        goToSleep();
                        processed = true;
                    }
                    
                    if( parts[ 0 ].equals( "null" ) ) {
                        d( "null command" );
                        processed = true;
                    }
                    
                    if( parts[ 0 ].equals( "system" ) ) {
                        Runtime r = Runtime.getRuntime();
                        String exec = cmd.substring( "system ".length() );
                        d( "Executing shell command: '" + exec + "'" );
                        r.exec( exec );
                        processed = true;
                    }
                }
                
                if( ! processed ) {
                    Log.e( LOG, "Failed to process input: " + cmd );
                    d( "Failed to process input: " + cmd );
                }
            }
        }catch( Exception e ) {
            Log.i( LOG, e.toString() );
        }
        
        d( "Finished" );
    }
    
    private void d( String s ) {
        if( debug ) {
            //System.out.println( s );
            Log.d( LOG, s );
        }
    }

    private void sendKeyCode( int code, int action ) {
        long now = SystemClock.uptimeMillis();
        KeyEvent k = new KeyEvent(now, now, action, code, 0);
        
        try {
            ( IWindowManager.Stub
                .asInterface( ServiceManager.getService( Context.WINDOW_SERVICE) ) )
                .injectKeyEvent( k , true );
        } catch (RemoteException e) {
            Log.i( LOG, e.toString() );
        }
    }
    
    private void goToSleep() {
        try {
            ( IPowerManager.Stub
                .asInterface( ServiceManager.getService( Context.POWER_SERVICE ) ) )
                .goToSleep( SystemClock.uptimeMillis() );
        } catch (RemoteException e) {
            Log.i( LOG, e.toString() );
        }
    }
}