droidVncServer/src/org/onaips/vnc/MainApplication.java

107 lines
2.6 KiB
Java
Raw Normal View History

package org.onaips.vnc;
2010-09-24 20:47:56 +01:00
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Application;
2010-12-14 22:20:30 +00:00
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.preference.PreferenceManager;
2010-09-24 20:47:56 +01:00
import android.util.Log;
public class MainApplication extends Application {
@Override
public void onCreate() {
2010-12-14 22:20:30 +00:00
super.onCreate();
2010-09-24 20:47:56 +01:00
2010-12-14 22:20:30 +00:00
if (firstRun())
createBinary();
2010-09-24 20:47:56 +01:00
}
2010-12-14 22:20:30 +00:00
public boolean firstRun()
{
int versionCode = 0;
try {
versionCode = getPackageManager()
.getPackageInfo(getPackageName(), PackageManager.GET_META_DATA)
.versionCode;
} catch (NameNotFoundException e) {
Log.e("VNC", "Package not found... Odd, since we're in that package...", e);
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int lastFirstRun = prefs.getInt("last_run", 0);
if (lastFirstRun >= versionCode) {
Log.d("VNC", "Not first run");
return false;
}
Log.d("VNC", "First run for version " + versionCode);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("last_run", versionCode);
editor.commit();
return true;
}
2010-09-24 20:47:56 +01:00
public void createBinary()
{
copyBinary(R.raw.androidvncserver, getFilesDir().getAbsolutePath() + "/androidvncserver");
copyBinary(R.raw.vncviewer, getFilesDir().getAbsolutePath()+"/VncViewer.jar");
copyBinary(R.raw.indexvnc, getFilesDir().getAbsolutePath()+"/index.vnc");
2010-12-14 22:20:30 +00:00
2010-09-24 20:47:56 +01:00
Process sh;
try {
sh = Runtime.getRuntime().exec("su");
OutputStream os = sh.getOutputStream();
writeCommand(os, "killall androidvncserver");
writeCommand(os, "killall -KILL androidvncserver");
2010-09-24 20:47:56 +01:00
//chmod 777 SHOULD exist
writeCommand(os, "chmod 777 " + getFilesDir().getAbsolutePath() + "/androidvncserver");
2010-12-14 22:20:30 +00:00
os.close();
2010-09-24 20:47:56 +01:00
} catch (IOException e) {
Log.v("VNC",e.getMessage());
}catch (Exception e) {
Log.v("VNC",e.getMessage());
}
}
public void copyBinary(int id,String path)
{
try {
InputStream ins = getResources().openRawResource(id);
int size = ins.available();
// Read the entire resource into a local byte buffer.
byte[] buffer = new byte[size];
ins.read(buffer);
ins.close();
FileOutputStream fos = new FileOutputStream(path);
fos.write(buffer);
fos.close();
}
catch (Exception e)
{
Log.v("VNC","public void createBinary(): " + e.getMessage());
}
}
static void writeCommand(OutputStream os, String command) throws Exception
{
os.write((command + "\n").getBytes("ASCII"));
}
2010-12-14 22:20:30 +00:00
2010-09-24 20:47:56 +01:00
}