summaryrefslogtreecommitdiffstats
path: root/src/net/hoopajoo/android/SoftKeys/Globals.java
blob: dc8d356d1ccf1dfd7ee9215f6d510cd87a8aa36d (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 *
 *  Copyright (c) 2010 Steve Slaven
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
*/
package net.hoopajoo.android.SoftKeys;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class Globals extends Application {
    private CommandShell cmd = null;
    private String android_id = null;
    private String LOG = "SoftKeys.Global";
    
    public boolean restartKeys = false;
    public int homeCounter = 0;
    public boolean didInitNotifications = false;
    public boolean firstRun = true;
    
    public CommandShell getCommandShell() throws Exception {
        if( cmd == null ) {
            if( android_id == null ) {
                // to run in the emulator
                // adb shell
                // # mkdir /data/tmp
                // # cat /system/bin/sh > /data/tmp/su
                // # chmod 6755 /data/tmp/su
                // # mount -oremount,suid /dev/block/mtdblock1 /data
                Log.d( LOG, "Detected emulator" );
                cmd = new CommandShell( "/data/tmp/su" );
            }else{
                cmd = new CommandShell( "su" );
            }
        }

        return( cmd );
    }

    // this is a string of keydown/keyup events by key id
    public int sendKeys( List<Integer> a ) {
        return sendKeys( listToInt( a ) );
    }
    
    // TODO: replace this with monkey script, then we can do longpresses
    public int sendKeys( int[] keyids ) {
        try {
            Globals.CommandShell cmd = getCommandShell();
            
            // run our key script
            String wd = getFilesDir().getAbsolutePath();

            // check if we have a dev script
            File script = new File( wd + "/pushkey.dev" );
            
            // check if we have a test script
            if( script.exists() ) {
                Log.d( LOG, "Using dev key script" );
            }else{
                // write out our default script
                script = new File( wd + "/pushkey" );
                FileOutputStream out = new FileOutputStream( script );
                out.write( "for f in $* ; do input keyevent $f ; done\n".getBytes( "ASCII" ) );
                out.close();
            }
            
            // source the file since datadata might be noexec
            StringBuilder keyid = new StringBuilder();
            for( int i = 0; i < keyids.length; i++ ) {
                keyid.append( " " );
                keyid.append( keyids[ i ] );
            }
            cmd.system( "sh " + script.getAbsolutePath() + " " + keyid );            
        }catch( Exception e ) {
            Log.e( LOG, "Error: " + e.getMessage() );
            Toast.makeText( this, "Unable to execute as root", Toast.LENGTH_LONG ).show();
            return 1;
        }
        
        return 0;
    }
    
    public class CommandShell {
        Process p;
        OutputStream o;
        
        CommandShell( String shell ) throws Exception {
            Log.d( "SoftKeys.cmdshell", "Starting shell: '" + shell + "'" );
            p = Runtime.getRuntime().exec( shell );
            o = p.getOutputStream();
        }
        
        public void system( String cmd ) throws Exception {
            Log.d( "SoftKeys.cmdshell", "Running command: '" + cmd + "'" );
            o.write(  (cmd + "\n" ).getBytes( "ASCII" ) );
        }
        
        public void close() throws Exception {
            Log.d( "SoftKeys.cmdshell", "Destroying shell" );
            o.flush();
            o.close();
            p.destroy();
        }
    }
    
    @Override
    public void onCreate() {
        // warn if we don't notice some binaries we need
        for( String name : new String[] { "/system/bin/su", "/system/bin/input" } ) {
            File check = new File( name );
            try {
                if( ! check.exists() ) {
                    Toast.makeText( this, "Failed to find file: " + name + ", SoftKeys may not function", Toast.LENGTH_LONG ).show();                    
                }
            }catch( Exception e ) {
                Toast.makeText( this, "Unable to check for file: " + name, Toast.LENGTH_LONG ).show();
            }
                
        }

        android_id = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
        restartService();
    }
    
    public void restartService() {
        // start the service
        this.stopService( new Intent( this, SoftKeysService.class ) );
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences( this );
        if( settings.getBoolean(  "service", true ) ) {
            this.startService( new Intent( this, SoftKeysService.class ) );
        }
    }
    
    private int[] listToInt( List<Integer> a ) {
        int[] ret = new int[ a.size() ];
        for( int i = 0; i < a.size(); i++ ) {
            ret[ i ] = a.get( i ).intValue();
        }
        return( ret );
    }
    
    public void doHomeAction( boolean longClick ) {
        // special case
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences( this );
        
        Intent ni = new Intent( Intent.ACTION_MAIN );
        String launcher = settings.getString( longClick ? "launcher2" : "launcher" , null );
        if( launcher == null ) {
            launcher = getDefaultLauncher();
        }
        ni.setPackage( launcher ); 
        ni.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity( ni );
    }
    
    private String getDefaultLauncher() {
        // Set default launcher to the first launcher we find so we don't freak out if it's not
        // set and there is no com.android.launcher
        Intent i = new Intent( Intent.ACTION_MAIN );
        i.addCategory( Intent.CATEGORY_HOME );
        PackageManager p = getPackageManager();
        List<ResolveInfo> packages = p.queryIntentActivities( i, 0 );
        
        String defaultLauncher = null;
        for( Iterator<ResolveInfo> it = packages.iterator(); it.hasNext(); ) {
            ResolveInfo info = it.next();
            if( defaultLauncher == null ) {
                if( ! info.activityInfo.applicationInfo.packageName.equals( "net.hoopajoo.android.SoftKeys" ) ) {
                    defaultLauncher = info.activityInfo.applicationInfo.packageName;
                }
            }
        }
        if( defaultLauncher == null ) {
            // last ditch
            defaultLauncher = "com.android.launcher";
        }
        return( defaultLauncher );
    }
}