手机box一体化,流程调通

chengzhenyu 7 年 前
コミット
2a84b1b244

+ 104 - 41
app/src/main/java/ai/pai/lensman/App.java

@@ -4,6 +4,8 @@ import android.app.Application;
4 4
 import android.content.Context;
5 5
 import android.widget.Toast;
6 6
 
7
+import com.android.common.utils.FileUnzipTask;
8
+import com.android.common.utils.LogHelper;
7 9
 import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
8 10
 import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
9 11
 import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
@@ -19,13 +21,14 @@ import java.io.File;
19 21
 import java.io.FileOutputStream;
20 22
 import java.io.IOException;
21 23
 import java.lang.reflect.Field;
24
+import java.util.ArrayList;
22 25
 
23 26
 import ai.pai.lensman.db.Preferences;
24 27
 import ai.pai.lensman.utils.Constants;
25 28
 import ai.pai.lensman.utils.PatchManager;
26 29
 import ai.pai.lensman.utils.UrlContainer;
27 30
 
28
-public class App extends Application {
31
+public class App extends Application{
29 32
 
30 33
     private static Context   mInstance;
31 34
 
@@ -40,7 +43,7 @@ public class App extends Application {
40 43
         if(!checkUsbPermission()){
41 44
             Toast.makeText(this,"申请usb权限失败",Toast.LENGTH_SHORT).show();
42 45
         }
43
-        Preferences.getInstance().setCameraLibExist(checkCameraLibs());
46
+        checkCameraLibs();
44 47
         initImageLoader();
45 48
         new PatchManager().patch(this, UrlContainer.PATCH_CONFIG_URL,Constants.APP_PATCH_DIR+ File.separator+"patch-"+BuildConfig.VERSION_NAME+".dex","ai.pai.lensman.patch.PatchesInfoImpl");
46 49
         noMedia();
@@ -73,23 +76,24 @@ public class App extends Application {
73 76
             }
74 77
         }
75 78
     }
79
+
76 80
     public void copyResToSdcard(String name){
77 81
         Field[] raw = R.raw.class.getFields();
78 82
         for (Field r : raw) {
79 83
             try {
80 84
                 int id=getResources().getIdentifier(r.getName(), "raw", getPackageName());
81
-                if(r.getName().startsWith("lib")){
85
+                if(r.getName().startsWith("camera")){
82 86
                     String path=name+"/"+r.getName()+".so";
83
-                    BufferedOutputStream bufEcrivain = new BufferedOutputStream((new FileOutputStream(new File(path))));
84
-                    BufferedInputStream VideoReader = new BufferedInputStream(getResources().openRawResource(id));
87
+                    BufferedOutputStream rawZipWriter = new BufferedOutputStream((new FileOutputStream(new File(path))));
88
+                    BufferedInputStream rawZipReader = new BufferedInputStream(getResources().openRawResource(id));
85 89
                     byte[] buff = new byte[20*1024];
86 90
                     int len;
87
-                    while( (len = VideoReader.read(buff)) > 0 ){
88
-                        bufEcrivain.write(buff,0,len);
91
+                    while( (len = rawZipReader.read(buff)) > 0 ){
92
+                        rawZipWriter.write(buff,0,len);
89 93
                     }
90
-                    bufEcrivain.flush();
91
-                    bufEcrivain.close();
92
-                    VideoReader.close();
94
+                    rawZipWriter.flush();
95
+                    rawZipWriter.close();
96
+                    rawZipReader.close();
93 97
                 }
94 98
             } catch (Exception e) {
95 99
                 e.printStackTrace();
@@ -98,48 +102,107 @@ public class App extends Application {
98 102
 
99 103
     }
100 104
 
101
-    private boolean checkCameraLibs(){
105
+    private void checkCameraLibs(){
102 106
         if(Preferences.getInstance().isCameraLibExist()){
103
-            return true;
107
+            return ;
104 108
         }
105
-        String tmpLibDirPath = "/mnt/sdcard/lensman/so";
109
+        String tmpLibDirPath = "/mnt/sdcard/lensman";
106 110
         File file = new File(tmpLibDirPath);
107 111
         file.mkdirs();
108 112
         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" ;
113
+        final String unzipPath = tmpLibDirPath+File.separator+"so";
114
+        new File(unzipPath).mkdirs();
115
+        new FileUnzipTask(tmpLibDirPath + File.separator + "camera.so", unzipPath, new FileUnzipTask.FileUnzipListener() {
116
+            @Override
117
+            public void onUnzipSuccess(String zipFilePath, String unZipDir) {
118
+                LogHelper.d("czy","onUnzipSuccess");
119
+                ArrayList<String> libNames = new ArrayList<>();
120
+                File libDir = new File(unZipDir);
121
+                for(File file : libDir.listFiles()){
122
+                    if(file.isFile()){
123
+                        libNames.add(file.getName());
124
+                    }else if(file.isDirectory()){
125
+                        for(File subFile : file.listFiles()){
126
+                            libNames.add(file.getName()+File.separator+subFile.getName());
127
+                        }
128
+                    }
129
+                }
130
+                Process process = null;
131
+                DataOutputStream os = null;
132
+                try {
133
+                    String cmd0 = "";
134
+                    for(String lib :libNames){
135
+                        cmd0+="rm /system/lib/"+lib+"\n";
136
+                    }
137
+                    LogHelper.d("czy","cmd0="+cmd0);
138
+                    String cmd = "chmod 777 " + getPackageCodePath();
139
+                    String cmd2 = "chmod -R 0777 /dev/bus/usb" ;
114 140
 
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" ;
141
+                    String cmd3 = "mount -o rw,remount /dev/block/mtdblock8 /system" ;
142
+                    String cmd5 = "cp -fr /mnt/sdcard/lensman/so/.  /system/lib" ;
118 143
 
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();
144
+                    String cmd6 = "" ;
145
+                    for(String lib :libNames){
146
+                        cmd6+="chmod 0777 /system/lib/"+lib+"\n";
147
+                    }
148
+                    LogHelper.d("czy","cmd6="+cmd6);
149
+                    process = Runtime.getRuntime().exec("su"); // 切换到root帐号
150
+                    os = new DataOutputStream(process.getOutputStream());
151
+                    os.writeBytes(cmd + "\n");
152
+                    os.writeBytes(cmd0);
153
+                    os.writeBytes(cmd2 + "\n");
154
+                    os.writeBytes(cmd3 + "\n");
155
+                    os.writeBytes(cmd5 + "\n");
156
+                    os.writeBytes(cmd6 + "\n");
157
+                    os.writeBytes("exit\n");
158
+                    os.flush();
159
+                    process.waitFor();
160
+                    Preferences.getInstance().setCameraLibExist(true);
161
+                } catch (Exception e) {
162
+                    Preferences.getInstance().setCameraLibExist(false);
163
+                } finally {
164
+                    try {
165
+                        if (os != null) {
166
+                            os.close();
167
+                        }
168
+                        process.destroy();
169
+                        new File(zipFilePath).delete();
170
+                        deleteDir(new File(unZipDir));
171
+                    } catch (Exception e) {
172
+                    }
136 173
                 }
137
-                process.destroy();
138
-            } catch (Exception e) {
139 174
             }
140
-        }
175
+
176
+            @Override
177
+            public void onUnzipError(int errorCode, String zipFilePath, String unZipDir) {
178
+                LogHelper.d("czy","onUnzipError");
179
+                new File(zipFilePath).delete();
180
+                deleteDir(new File(unZipDir));
181
+            }
182
+        }).startUnZip();
183
+
141 184
     }
142 185
 
186
+    private static boolean deleteDir(File dir) {
187
+        if (dir.isDirectory()) {
188
+            String[] children = dir.list();
189
+            if (children != null) {
190
+                // 递归删除目录中的子目录下
191
+                for (int i = 0; i < children.length; i++) {
192
+                    boolean success = deleteDir(new File(dir, children[i]));
193
+                    if (!success) {
194
+                        return false;
195
+                    }
196
+                }
197
+            }
198
+
199
+        }
200
+        if (!dir.canRead() || !dir.canWrite()) {
201
+            return false;
202
+        }
203
+        // 目录此时为空,可以删除
204
+        return dir.delete();
205
+    }
143 206
 
144 207
     private void noMedia(){
145 208
         File noMedia = new File(Constants.APP_ROOT_DIR,".nomedia");

+ 16 - 2
app/src/main/java/ai/pai/lensman/dslr/CameraJNIInterface.java

@@ -2,14 +2,28 @@ package ai.pai.lensman.dslr;
2 2
 
3 3
 public class CameraJNIInterface {
4 4
 
5
+
5 6
     static {
6
-        System.loadLibrary("");
7
+        System.loadLibrary("hello_jni");
8
+    }
9
+
10
+    private static CameraJNIInterface instance;
11
+
12
+    private CameraJNIInterface(){
13
+
14
+    }
15
+
16
+    public static synchronized CameraJNIInterface getInstance(){
17
+        if(instance==null){
18
+            instance = new CameraJNIInterface();
19
+        }
20
+        return instance;
7 21
     }
8 22
 
9 23
     public native  int  mygpcamerainit();
10 24
     public native  int  mygpcameraexit();
11 25
     public native  String  mygpcameragetsummary();
12
-    public native  int  mygpcamerawaitforevent(String  foldr_path, String file_name);
26
+    public native  String  mygpcamerawaitforevent(String  foldr_path);
13 27
     public native  int  mygpfilenewfromfd(String  file_path);
14 28
     public native  int  mygpcamerafileget(String  foldr_path, String file_name);
15 29
     public native  int  mygpfilefree();

+ 55 - 3
app/src/main/java/ai/pai/lensman/login/LoginActivity.java

@@ -1,22 +1,27 @@
1 1
 package ai.pai.lensman.login;
2 2
 
3 3
 import android.content.Intent;
4
-import android.graphics.Paint;
5 4
 import android.os.Bundle;
6 5
 import android.support.v4.app.FragmentActivity;
7
-import android.text.Html;
8 6
 import android.text.SpannableString;
9 7
 import android.text.TextUtils;
10 8
 import android.text.style.UnderlineSpan;
11 9
 import android.view.View;
10
+import android.widget.ImageView;
12 11
 import android.widget.TextView;
13 12
 import android.widget.Toast;
14 13
 
14
+import com.android.common.utils.LogHelper;
15 15
 import com.android.views.progressbar.ProgressWheel;
16 16
 
17
+import java.io.File;
18
+
17 19
 import ai.pai.lensman.R;
18 20
 import ai.pai.lensman.activities.WebViewActivity;
21
+import ai.pai.lensman.dslr.CameraJNIInterface;
19 22
 import ai.pai.lensman.main.MainActivity;
23
+import ai.pai.lensman.service.Constants;
24
+import ai.pai.lensman.utils.ImageLoaderUtils;
20 25
 import butterknife.BindView;
21 26
 import butterknife.ButterKnife;
22 27
 import butterknife.OnClick;
@@ -25,8 +30,9 @@ public class LoginActivity extends FragmentActivity implements LoginContract.Vie
25 30
 
26 31
     @BindView(R.id.wheel_wait_http)    ProgressWheel progressWheel;
27 32
     @BindView(R.id.tv_agree_protocol)  TextView protocolText;
28
-
33
+    @BindView(R.id.iv_login_logo)      ImageView photoView;
29 34
     private LoginContract.Presenter presenter;
35
+    private boolean isCancelled;
30 36
 
31 37
     @Override
32 38
     protected void onCreate(Bundle savedInstanceState) {
@@ -37,8 +43,47 @@ public class LoginActivity extends FragmentActivity implements LoginContract.Vie
37 43
         content.setSpan(new UnderlineSpan(), 6, content.length(), 0);
38 44
         protocolText.setText(content);
39 45
         presenter = new LoginPresenter(this,this);
46
+        int ret = CameraJNIInterface.getInstance().mygpcamerainit();
47
+        if(ret>=0){
48
+            String text = CameraJNIInterface.getInstance().mygpcameragetsummary();
49
+            LogHelper.d("czy","text="+text);
50
+            protocolText.setText("text="+text);
51
+//            Toast.makeText(this,text,Toast.LENGTH_LONG).show();
52
+        }else{
53
+            LogHelper.d("czy","ret="+ret);
54
+//            Toast.makeText(this,"camera init return "+ret,Toast.LENGTH_LONG).show();
55
+            protocolText.setText("camera init return "+ret);
56
+        }
57
+        File dir = new File(Constants.APP_IMAGE_DIR);
58
+        dir.mkdirs();
59
+        fetchPhotoTask();
40 60
     }
41 61
 
62
+    private void fetchPhotoTask(){
63
+        new Thread(new Runnable() {
64
+            @Override
65
+            public void run() {
66
+                while (!isCancelled){
67
+                    final String photoName = CameraJNIInterface.getInstance().mygpcamerawaitforevent(Constants.APP_IMAGE_DIR);
68
+                    LogHelper.d("czy","mygpcamerawaitforevent return "+photoName);
69
+                    photoView.post(new Runnable() {
70
+                        @Override
71
+                        public void run() {
72
+                            protocolText.setText("mygpcamerawaitforevent return "+photoName);
73
+                            if(photoName!=null && photoName.length()>0){
74
+                                String sub = photoName.substring(0,1);
75
+                                if(TextUtils.isDigitsOnly(sub)){
76
+                                    ImageLoaderUtils.displayLocalImage(Constants.APP_IMAGE_DIR+File.separator+photoName,photoView,ImageLoaderUtils.getOptions(R.drawable.login_page_logo));
77
+                                }
78
+                            }
79
+
80
+                        }
81
+                    });
82
+                }
83
+            }
84
+        }).start();
85
+
86
+    }
42 87
 
43 88
     @Override
44 89
     protected void onResume() {
@@ -46,6 +91,13 @@ public class LoginActivity extends FragmentActivity implements LoginContract.Vie
46 91
         presenter.start();
47 92
     }
48 93
 
94
+    @Override
95
+    protected void onDestroy() {
96
+        super.onDestroy();
97
+        CameraJNIInterface.getInstance().mygpcameraexit();
98
+        isCancelled = true;
99
+    }
100
+
49 101
     @OnClick(R.id.btn_login)
50 102
     public void login(){
51 103
         presenter.login();

+ 12 - 1
app/src/main/java/ai/pai/lensman/session/SessionInteractor.java

@@ -1,15 +1,19 @@
1 1
 package ai.pai.lensman.session;
2 2
 
3
+import android.widget.Toast;
4
+
3 5
 import com.android.common.utils.LogHelper;
4 6
 
5 7
 import java.util.ArrayList;
6 8
 import java.util.Timer;
7 9
 import java.util.TimerTask;
8 10
 
11
+import ai.pai.lensman.App;
9 12
 import ai.pai.lensman.BuildConfig;
10 13
 import ai.pai.lensman.bean.PhotoBean;
11 14
 import ai.pai.lensman.bean.SessionBean;
12 15
 import ai.pai.lensman.db.DBService;
16
+import ai.pai.lensman.dslr.CameraJNIInterface;
13 17
 
14 18
 public class SessionInteractor {
15 19
 
@@ -55,6 +59,12 @@ public class SessionInteractor {
55 59
     }
56 60
 
57 61
     public void startCapture() {
62
+        int result = CameraJNIInterface.getInstance().mygpcamerainit();
63
+        if(result>=0){
64
+            LogHelper.d(TAG,"相机信息--->\n"+CameraJNIInterface.getInstance().mygpcameragetsummary());
65
+            Toast.makeText(App.getAppContext(),CameraJNIInterface.getInstance().mygpcameragetsummary(),Toast.LENGTH_SHORT).show();
66
+
67
+        }
58 68
         if(timer!=null){
59 69
             timer.cancel();
60 70
             timer = null;
@@ -65,13 +75,14 @@ public class SessionInteractor {
65 75
             public void run() {
66 76
                 fetchPhotoTask();
67 77
             }
68
-        },5000,2000);
78
+        },5000,1000);
69 79
     }
70 80
 
71 81
     private void fetchPhotoTask(){
72 82
         if(!isWorking){
73 83
             return;
74 84
         }
85
+
75 86
         if(!isLastQueryReturned){
76 87
             return;
77 88
         }

BIN
app/src/main/res/raw/camera.so


+ 110 - 0
common/src/main/java/com/android/common/utils/FileUnzipTask.java

@@ -0,0 +1,110 @@
1
+package com.android.common.utils;
2
+
3
+import android.os.AsyncTask;
4
+
5
+import java.io.File;
6
+import java.io.FileOutputStream;
7
+import java.io.InputStream;
8
+import java.io.OutputStream;
9
+import java.util.Enumeration;
10
+import java.util.zip.ZipFile;
11
+
12
+/**
13
+ * zip文件解压缩任务,支持zip内多级目录结构
14
+ *
15
+ * Created By chengzhenyu
16
+ */
17
+public class FileUnzipTask {
18
+
19
+    private String unZipDir;
20
+    private String zipFilePath;
21
+    private AsyncTask unzipTask;
22
+    private FileUnzipListener listener;
23
+
24
+    private boolean isCancelled;
25
+
26
+    private static final int BUF_SIZE = 2048;
27
+
28
+    private static final String TAG = "FileUnzipTask";
29
+
30
+    public interface FileUnzipListener {
31
+        void onUnzipSuccess(String zipFilePath, String unZipDir);
32
+        void onUnzipError(int errorCode, String zipFilePath, String unZipDir);
33
+    }
34
+
35
+    public FileUnzipTask(String zipFilePath, String unZipDir, FileUnzipListener listener) {
36
+        this.listener = listener;
37
+        this.unZipDir = unZipDir;
38
+        this.zipFilePath = zipFilePath;
39
+    }
40
+
41
+    public void startUnZip() {
42
+        isCancelled = false;
43
+
44
+        unzipTask = new AsyncTask<Object, Integer, Boolean>() {
45
+
46
+            @Override
47
+            protected Boolean doInBackground(Object... params) {
48
+                try {
49
+                    File desDir = new File(unZipDir);
50
+                    if (!desDir.exists()) {
51
+                        desDir.mkdirs();
52
+                    }
53
+                    ZipFile zf = new ZipFile(zipFilePath);
54
+                    for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
55
+                        if(isCancelled){
56
+                            break;
57
+                        }
58
+                        java.util.zip.ZipEntry entry = ((java.util.zip.ZipEntry)entries.nextElement());
59
+                        InputStream in = zf.getInputStream(entry);
60
+                        String str = unZipDir + File.separator + entry.getName();
61
+                        str = new String(str.getBytes("8859_1"), "GB2312");
62
+                        File desFile = new File(str);
63
+                        if (!desFile.exists()) {
64
+                            File fileParentDir = desFile.getParentFile();
65
+                            if (!fileParentDir.exists()) {
66
+                                fileParentDir.mkdirs();
67
+                            }
68
+                            if(entry.isDirectory()){
69
+                                desFile.mkdirs();
70
+                                continue;
71
+                            }else{
72
+                                desFile.createNewFile();
73
+                            }
74
+                        }
75
+                        OutputStream out = new FileOutputStream(desFile);
76
+                        byte buffer[] = new byte[BUF_SIZE];
77
+                        int realLength;
78
+                        while ((realLength = in.read(buffer)) > 0) {
79
+                            out.write(buffer, 0, realLength);
80
+                        }
81
+                        in.close();
82
+                        out.close();
83
+                    }
84
+                    return true;
85
+                } catch (Exception e) {
86
+                    e.printStackTrace();
87
+                }
88
+                return false;
89
+            }
90
+
91
+            @Override
92
+            protected void onPostExecute(Boolean result) {
93
+                LogHelper.d(TAG, "unzip task onPostExecute result=" + result);
94
+                if (result) {
95
+                    listener.onUnzipSuccess(zipFilePath, unZipDir);
96
+                } else {
97
+                    listener.onUnzipError(-1, zipFilePath, unZipDir);
98
+                }
99
+            }
100
+        };
101
+        unzipTask.execute();
102
+    }
103
+
104
+    public void cancelUnzip() {
105
+        if (unzipTask != null) {
106
+            isCancelled = true;
107
+            unzipTask.cancel(true);
108
+        }
109
+    }
110
+}