OSDN Git Service

launch the apk after download
authoronuxy <n.nanatunoko@gmail.com>
Sun, 29 Jul 2012 15:32:13 +0000 (00:32 +0900)
committeronuxy <n.nanatunoko@gmail.com>
Sun, 29 Jul 2012 15:32:13 +0000 (00:32 +0900)
CUTEn/src/jp/ac/titech/sharp4k/cuten/DownloadApkListener.java [new file with mode: 0644]
CUTEn/src/jp/ac/titech/sharp4k/cuten/DownloadApkTask.java
CUTEn/src/jp/ac/titech/sharp4k/cuten/LectureActivity.java

diff --git a/CUTEn/src/jp/ac/titech/sharp4k/cuten/DownloadApkListener.java b/CUTEn/src/jp/ac/titech/sharp4k/cuten/DownloadApkListener.java
new file mode 100644 (file)
index 0000000..1b29832
--- /dev/null
@@ -0,0 +1,7 @@
+package jp.ac.titech.sharp4k.cuten;
+
+import java.io.File;
+
+public interface DownloadApkListener {
+       void onDownloadFinished(int id,File file);
+}
index bd7aa55..8a6e803 100644 (file)
@@ -34,6 +34,8 @@ public class DownloadApkTask extends AsyncTask<String, Integer, File> {
        private ProgressDialog progressDialog;
        private Context context;
        public String savePath;
+       private DownloadApkListener listener;
+       private int id;
        private Handler handler = new Handler() {
                public void handleMessage(Message msg) {
                        String str = (String) msg.obj;
@@ -41,9 +43,12 @@ public class DownloadApkTask extends AsyncTask<String, Integer, File> {
                }
        };
 
-       public DownloadApkTask(Activity activity) {
-               context = activity;
+       public DownloadApkTask(Context context,
+                       DownloadApkListener downloadListener, int id) {
+               this.context = context;
                savePath = context.getFilesDir().getPath();
+               listener = downloadListener;
+               this.id = id;
        }
 
        @Override
@@ -124,5 +129,6 @@ public class DownloadApkTask extends AsyncTask<String, Integer, File> {
                        msg.obj = "ダウンロード完了!!";
                        handler.sendMessage(msg);
                }
+               listener.onDownloadFinished(id,file);
        }
 }
index 0890011..cefb2b2 100644 (file)
@@ -16,9 +16,11 @@ import android.widget.LinearLayout;
 import android.widget.Toast;
 
 public class LectureActivity extends BaseMenuActivity implements
-               OnClickListener {
+               OnClickListener, DownloadApkListener {
        private static final String TAG = "LectureActivity";
        private static final int TASK_REQUEST_CODE = 9;
+       Task task;
+       Apk apk;
 
        @Override
        public void onCreate(Bundle savedInstanceState) {
@@ -88,22 +90,17 @@ public class LectureActivity extends BaseMenuActivity implements
                SQLiteOpenHelper h = new SQLHelper(this);
                final SQLiteDatabase db = h.getReadableDatabase();
                // DBから名前取得
-               Task task = Task.find(db, id);
-               Apk apk = task.findLatestApk(db);
+               task = Task.find(db, id);
+               apk = task.findLatestApk(db);
                if (apk == null) {
                        Log.d(TAG, "apk id = " + id.toString() + " : not found");
                        return;
                }
                db.close();
                //
-               downloadApk(id);
-               String apkName = id.toString() + ".apk";
-               Intent intent = new Intent(this, TaskActivity.class);
-               intent.putExtra(TaskActivity.TASK_NAME_KEY, task.getName());
-               intent.putExtra(TaskActivity.APK_NAME_KEY, apkName);
-               intent.putExtra(TaskActivity.QUAL_NAME_KEY, apk.getClassName());
-               Log.d(TAG, "startActivity: " + apkName + ":" + apk.getClassName());
-               startActivityForResult(intent, TASK_REQUEST_CODE);
+               if (!downloadApk(id)) {
+                       launchApk(id);
+               }
        }
 
        @Override
@@ -133,13 +130,32 @@ public class LectureActivity extends BaseMenuActivity implements
                }
        }
 
-       private void downloadApk(Integer id) {
-               DownloadApkTask task = new DownloadApkTask(this);
+       private boolean downloadApk(Integer id) {
+               DownloadApkTask task = new DownloadApkTask(this, this, id);
                File file = new File(task.savePath + "/" + id.toString() + ".apk");
                if (!file.exists()) {
                        task.execute(
                                        "http://www16307ue.sakura.ne.jp:3001/apks/" + id.toString()
                                                        + "/download", id.toString() + ".apk");
+                       return true;
                }
+               return false;
+       }
+
+       @Override
+       public void onDownloadFinished(int id, File file) {
+               if (file != null) {
+                       launchApk(id);
+               }
+       }
+
+       private void launchApk(Integer id) {
+               String apkName = id.toString() + ".apk";
+               Intent intent = new Intent(this, TaskActivity.class);
+               intent.putExtra(TaskActivity.TASK_NAME_KEY, task.getName());
+               intent.putExtra(TaskActivity.APK_NAME_KEY, apkName);
+               intent.putExtra(TaskActivity.QUAL_NAME_KEY, apk.getClassName());
+               Log.d(TAG, "startActivity: " + apkName + ":" + apk.getClassName());
+               startActivityForResult(intent, TASK_REQUEST_CODE);
        }
 }