github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/clients/android/src/org/camlistore/UploadApplication.java (about) 1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package org.camlistore; 18 19 import java.io.BufferedReader; 20 import java.io.File; 21 import java.io.FileOutputStream; 22 import java.io.IOException; 23 import java.io.InputStream; 24 import java.io.InputStreamReader; 25 import java.lang.reflect.Method; 26 27 import android.app.Application; 28 import android.content.pm.PackageManager.NameNotFoundException; 29 import android.util.Log; 30 31 public class UploadApplication extends Application { 32 private final static String TAG = "UploadApplication"; 33 private final static boolean STRICT_MODE = true; 34 35 private long getAPKModTime() { 36 try { 37 return getPackageManager().getPackageInfo(getPackageName(), 0).lastUpdateTime; 38 } catch (NameNotFoundException e) { 39 throw new RuntimeException(e); 40 } 41 } 42 43 private void copyGoBinary() { 44 long myTime = getAPKModTime(); 45 String dstFile = getBaseContext().getFilesDir().getAbsolutePath() + "/camput.bin"; 46 File f = new File(dstFile); 47 Log.d(TAG, " My Time: " + myTime); 48 Log.d(TAG, "Bin Time: " + f.lastModified()); 49 if (f.exists() && f.lastModified() > myTime) { 50 Log.d(TAG, "Go binary modtime up-to-date."); 51 return; 52 } 53 Log.d(TAG, "Go binary missing or modtime stale. Re-copying from APK."); 54 try { 55 InputStream is = getAssets().open("camput.arm"); 56 FileOutputStream fos = getBaseContext().openFileOutput("camput.bin.writing", MODE_PRIVATE); 57 byte[] buf = new byte[8192]; 58 int offset; 59 while ((offset = is.read(buf)) > 0) { 60 fos.write(buf, 0, offset); 61 } 62 is.close(); 63 fos.flush(); 64 fos.close(); 65 66 String writingFilePath = dstFile + ".writing"; 67 Log.d(TAG, "wrote out " + writingFilePath); 68 Runtime.getRuntime().exec("chmod 0777 " + writingFilePath); 69 Log.d(TAG, "did chmod 0700 on " + writingFilePath); 70 Runtime.getRuntime().exec("mv " + writingFilePath + " " + dstFile); 71 f = new File(dstFile); 72 f.setLastModified(myTime); 73 Log.d(TAG, "set modtime of " + dstFile); 74 } catch (IOException e) { 75 throw new RuntimeException(e); 76 } 77 } 78 79 @Override 80 public void onCreate() { 81 super.onCreate(); 82 83 copyGoBinary(); 84 85 if (!STRICT_MODE) { 86 Log.d(TAG, "Starting UploadApplication; release build."); 87 return; 88 } 89 90 try { 91 Runtime.getRuntime().exec("chmod 0755 " + getCacheDir().getAbsolutePath()); 92 } catch (IOException e) { 93 Log.d(TAG, "failed to chmod cache dir"); 94 } 95 96 try { 97 Class strictmode = Class.forName("android.os.StrictMode"); 98 Log.d(TAG, "StrictMode class found."); 99 Method method = strictmode.getMethod("enableDefaults"); 100 Log.d(TAG, "enableDefaults method found."); 101 method.invoke(null); 102 } catch (ClassNotFoundException e) { 103 } catch (LinkageError e) { 104 } catch (IllegalAccessException e) { 105 } catch (NoSuchMethodException e) { 106 } catch (SecurityException e) { 107 } catch (java.lang.reflect.InvocationTargetException e) { 108 } 109 } 110 111 public String getCamputVersion() { 112 InputStream is = null; 113 try { 114 is = getAssets().open("camput-version.txt"); 115 BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); 116 return br.readLine(); 117 } catch (IOException e) { 118 return e.toString(); 119 } finally { 120 if (is != null) { 121 try { 122 is.close(); 123 } catch (IOException e) { 124 } 125 } 126 } 127 } 128 }