| @@ -2,6 +2,7 @@ package ai.pai.lensman; | ||
| 2 | 2 |  | 
| 3 | 3 | import android.app.Application; | 
| 4 | 4 | import android.content.Context; | 
| 5 | +import android.widget.Toast; | |
| 5 | 6 |  | 
| 6 | 7 | import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache; | 
| 7 | 8 | import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; | 
| @@ -11,9 +12,15 @@ import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; | ||
| 11 | 12 | import com.nostra13.universalimageloader.core.assist.QueueProcessingType; | 
| 12 | 13 | import com.nostra13.universalimageloader.utils.StorageUtils; | 
| 13 | 14 |  | 
| 15 | +import java.io.BufferedInputStream; | |
| 16 | +import java.io.BufferedOutputStream; | |
| 17 | +import java.io.DataOutputStream; | |
| 14 | 18 | import java.io.File; | 
| 19 | +import java.io.FileOutputStream; | |
| 15 | 20 | import java.io.IOException; | 
| 21 | +import java.lang.reflect.Field; | |
| 16 | 22 |  | 
| 23 | +import ai.pai.lensman.db.Preferences; | |
| 17 | 24 | import ai.pai.lensman.utils.Constants; | 
| 18 | 25 | import ai.pai.lensman.utils.PatchManager; | 
| 19 | 26 | import ai.pai.lensman.utils.UrlContainer; | 
| @@ -29,12 +36,111 @@ public class App extends Application { | ||
| 29 | 36 | @Override | 
| 30 | 37 |      public void onCreate() { | 
| 31 | 38 | super.onCreate(); | 
| 32 | - initImageLoader(); | |
| 33 | 39 | mInstance = this.getApplicationContext(); | 
| 40 | +        if(!checkUsbPermission()){ | |
| 41 | + Toast.makeText(this,"申请usb权限失败",Toast.LENGTH_SHORT).show(); | |
| 42 | + } | |
| 43 | + Preferences.getInstance().setCameraLibExist(checkCameraLibs()); | |
| 44 | + initImageLoader(); | |
| 34 | 45 | new PatchManager().patch(this, UrlContainer.PATCH_CONFIG_URL,Constants.APP_PATCH_DIR+ File.separator+"patch-"+BuildConfig.VERSION_NAME+".dex","ai.pai.lensman.patch.PatchesInfoImpl"); | 
| 35 | 46 | noMedia(); | 
| 36 | 47 | } | 
| 37 | 48 |  | 
| 49 | +    private boolean checkUsbPermission(){ | |
| 50 | + Process process = null; | |
| 51 | + DataOutputStream os = null; | |
| 52 | +        try { | |
| 53 | + String cmd = "chmod 777 " + getPackageCodePath(); | |
| 54 | + String cmd2 = "chmod -R 0777 /dev/bus/usb" ; | |
| 55 | + | |
| 56 | +            process = Runtime.getRuntime().exec("su"); // 切换到root帐号 | |
| 57 | + os = new DataOutputStream(process.getOutputStream()); | |
| 58 | + os.writeBytes(cmd + "\n"); | |
| 59 | + os.writeBytes(cmd2 + "\n"); | |
| 60 | +            os.writeBytes("exit\n"); | |
| 61 | + os.flush(); | |
| 62 | + process.waitFor(); | |
| 63 | + return true; | |
| 64 | +        } catch (Exception e) { | |
| 65 | + return false; | |
| 66 | +        } finally { | |
| 67 | +            try { | |
| 68 | +                if (os != null) { | |
| 69 | + os.close(); | |
| 70 | + } | |
| 71 | + process.destroy(); | |
| 72 | +            } catch (Exception e) { | |
| 73 | + } | |
| 74 | + } | |
| 75 | + } | |
| 76 | +    public void copyResToSdcard(String name){ | |
| 77 | + Field[] raw = R.raw.class.getFields(); | |
| 78 | +        for (Field r : raw) { | |
| 79 | +            try { | |
| 80 | + int id=getResources().getIdentifier(r.getName(), "raw", getPackageName()); | |
| 81 | +                if(r.getName().startsWith("lib")){ | |
| 82 | + String path=name+"/"+r.getName()+".so"; | |
| 83 | + BufferedOutputStream bufEcrivain = new BufferedOutputStream((new FileOutputStream(new File(path)))); | |
| 84 | + BufferedInputStream VideoReader = new BufferedInputStream(getResources().openRawResource(id)); | |
| 85 | + byte[] buff = new byte[20*1024]; | |
| 86 | + int len; | |
| 87 | +                    while( (len = VideoReader.read(buff)) > 0 ){ | |
| 88 | + bufEcrivain.write(buff,0,len); | |
| 89 | + } | |
| 90 | + bufEcrivain.flush(); | |
| 91 | + bufEcrivain.close(); | |
| 92 | + VideoReader.close(); | |
| 93 | + } | |
| 94 | +            } catch (Exception e) { | |
| 95 | + e.printStackTrace(); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + } | |
| 100 | + | |
| 101 | +    private boolean checkCameraLibs(){ | |
| 102 | +        if(Preferences.getInstance().isCameraLibExist()){ | |
| 103 | + return true; | |
| 104 | + } | |
| 105 | + String tmpLibDirPath = "/mnt/sdcard/lensman/so"; | |
| 106 | + File file = new File(tmpLibDirPath); | |
| 107 | + file.mkdirs(); | |
| 108 | + copyResToSdcard(tmpLibDirPath); | |
| 109 | + Process process = null; | |
| 110 | + DataOutputStream os = null; | |
| 111 | +        try { | |
| 112 | + String cmd = "chmod 777 " + getPackageCodePath(); | |
| 113 | + String cmd2 = "chmod -R 0777 /dev/bus/usb" ; | |
| 114 | + | |
| 115 | + String cmd3 = "mount -o rw,remount /dev/block/mtdblock8 /system" ; | |
| 116 | + String cmd5 = "cp -f /mnt/sdcard/lensman/*.so /data/data/ai.pai.lensman" ; | |
| 117 | + String cmd6 = "chmod 0777 /data/data/ai.pai.lensman/*.so" ; | |
| 118 | + | |
| 119 | +            process = Runtime.getRuntime().exec("su"); // 切换到root帐号 | |
| 120 | + os = new DataOutputStream(process.getOutputStream()); | |
| 121 | + os.writeBytes(cmd + "\n"); | |
| 122 | + os.writeBytes(cmd2 + "\n"); | |
| 123 | + os.writeBytes(cmd3 + "\n"); | |
| 124 | + os.writeBytes(cmd5 + "\n"); | |
| 125 | + os.writeBytes(cmd6 + "\n"); | |
| 126 | +            os.writeBytes("exit\n"); | |
| 127 | + os.flush(); | |
| 128 | + process.waitFor(); | |
| 129 | + return true; | |
| 130 | +        } catch (Exception e) { | |
| 131 | + return false; | |
| 132 | +        } finally { | |
| 133 | +            try { | |
| 134 | +                if (os != null) { | |
| 135 | + os.close(); | |
| 136 | + } | |
| 137 | + process.destroy(); | |
| 138 | +            } catch (Exception e) { | |
| 139 | + } | |
| 140 | + } | |
| 141 | + } | |
| 142 | + | |
| 143 | + | |
| 38 | 144 |      private void noMedia(){ | 
| 39 | 145 | File noMedia = new File(Constants.APP_ROOT_DIR,".nomedia"); | 
| 40 | 146 |          if(noMedia.exists()){ | 
| @@ -56,20 +56,12 @@ public class Preferences { | ||
| 56 | 56 |          mPrefs.edit().putString("wxcode",wxCode).commit(); | 
| 57 | 57 | } | 
| 58 | 58 |  | 
| 59 | -    public void setPrinterMac(String macAdr){ | |
| 60 | -        mPrefs.edit().putString("mac",macAdr).commit(); | |
| 59 | +    public boolean isCameraLibExist(){ | |
| 60 | +        return mPrefs.getBoolean("CameraLibExist",false); | |
| 61 | 61 | } | 
| 62 | 62 |  | 
| 63 | -    public String getPrinterMac(){ | |
| 64 | -        return mPrefs.getString("mac",NullStr); | |
| 65 | - } | |
| 66 | - | |
| 67 | -    public boolean isPrinterConnected(){ | |
| 68 | -        return mPrefs.getBoolean("isPrinterConnected",false); | |
| 69 | - } | |
| 70 | - | |
| 71 | -    public void setPrinterConnected(boolean isPrinterConnect){ | |
| 72 | -        mPrefs.edit().putBoolean("isPrinterConnected",isPrinterConnect).commit(); | |
| 63 | +    public void setCameraLibExist(boolean CameraLibExist){ | |
| 64 | +        mPrefs.edit().putBoolean("CameraLibExist",CameraLibExist).commit(); | |
| 73 | 65 | } | 
| 74 | 66 |      public String getCurrentSession(){ | 
| 75 | 67 |          return mPrefs.getString("currentSession", NullStr); | 
| @@ -96,13 +88,6 @@ public class Preferences { | ||
| 96 | 88 |          return mPrefs.getInt("delay",10); | 
| 97 | 89 | } | 
| 98 | 90 |  | 
| 99 | -    public int getBoxVersionCode(){ | |
| 100 | -        return mPrefs.getInt("boxVersionCode",0); | |
| 101 | - } | |
| 102 | - | |
| 103 | -    public void setBoxVersionCode(int boxVersionCode){ | |
| 104 | -        mPrefs.edit().putInt("boxVersionCode",boxVersionCode).commit(); | |
| 105 | - } | |
| 106 | 91 |  | 
| 107 | 92 |      public void clearPrefs(){ | 
| 108 | 93 | mPrefs.edit().clear().commit(); | 
| @@ -29,7 +29,7 @@ public class SplashPresenter implements SplashContract.Presenter { | ||
| 29 | 29 |  | 
| 30 | 30 | @Override | 
| 31 | 31 |      public void start() { | 
| 32 | - Preferences.getInstance().setPrinterConnected(false); | |
| 32 | + | |
| 33 | 33 |          if(TextUtils.isEmpty(Preferences.getInstance().getLensManId())){ | 
| 34 | 34 |              mHandler.postDelayed(new Runnable() { | 
| 35 | 35 | @Override |