Mercurial > 510Connectbot
comparison src/com/five_ten_sg/connectbot/util/TransferThread.java @ 0:0ce5cc452d02
initial version
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 22 May 2014 10:41:19 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0ce5cc452d02 |
---|---|
1 /* | |
2 * TransferThread Class for VX ConnectBot | |
3 * Copyright 2012 Martin Matuska | |
4 * | |
5 * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 * you may not use this file except in compliance with the License. | |
7 * You may obtain a copy of the License at | |
8 * | |
9 * http://www.apache.org/licenses/LICENSE-2.0 | |
10 * | |
11 * Unless required by applicable law or agreed to in writing, software | |
12 * distributed under the License is distributed on an "AS IS" BASIS, | |
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 * See the License for the specific language governing permissions and | |
15 * limitations under the License. | |
16 */ | |
17 package com.five_ten_sg.connectbot.util; | |
18 | |
19 import java.util.StringTokenizer; | |
20 | |
21 import com.five_ten_sg.connectbot.R; | |
22 import com.five_ten_sg.connectbot.service.TerminalBridge; | |
23 import android.app.Activity; | |
24 import android.app.AlertDialog; | |
25 import android.app.ProgressDialog; | |
26 import android.content.SharedPreferences; | |
27 import android.content.res.Resources; | |
28 import android.os.Handler; | |
29 import android.preference.PreferenceManager; | |
30 import android.util.Log; | |
31 import android.widget.Toast; | |
32 | |
33 public class TransferThread extends Thread { | |
34 public final static String TAG = "ConnectBot.TransferThread"; | |
35 private final Activity activity; | |
36 private final SharedPreferences prefs; | |
37 private String dialogMessage = null; | |
38 private Handler handler = new Handler(); | |
39 private TerminalBridge bridge; | |
40 private String files, destName, destFolder; | |
41 private ProgressDialog progress = null; | |
42 private boolean upload; | |
43 private Toast progressToast = null; | |
44 | |
45 public TransferThread(Activity activity, Handler handler) { | |
46 this.activity = activity; | |
47 // this.handler = handler; | |
48 this.prefs = PreferenceManager.getDefaultSharedPreferences(this.activity); | |
49 } | |
50 | |
51 public void setProgressDialogMessage(String message) { | |
52 this.dialogMessage = message; | |
53 } | |
54 | |
55 public void download(TerminalBridge bridge, String files, String destName, String destFolder) { | |
56 this.bridge = bridge; | |
57 this.files = files; | |
58 this.destName = destName; | |
59 this.destFolder = destFolder; | |
60 this.upload = false; | |
61 this.configureProgressDialog(); | |
62 this.start(); | |
63 } | |
64 | |
65 public void upload(TerminalBridge bridge, String files, String destName, String destFolder) { | |
66 this.bridge = bridge; | |
67 this.files = files; | |
68 this.destName = destName; | |
69 this.destFolder = destFolder; | |
70 this.upload = true; | |
71 this.configureProgressDialog(); | |
72 this.start(); | |
73 } | |
74 | |
75 @Override | |
76 public void run() { | |
77 if (this.activity == null || this.handler == null || this.bridge == null) | |
78 return; | |
79 | |
80 Log.d(TAG, "Requested " + (upload ? "upload" : "download") + " of [" + files + "]"); | |
81 Resources res = activity.getResources(); | |
82 String fail = ""; | |
83 | |
84 try { | |
85 StringTokenizer fileSet = new StringTokenizer(files, "\n"); | |
86 | |
87 while (fileSet.hasMoreTokens()) { | |
88 String file = fileSet.nextToken(); | |
89 final String newMessage = res.getString(upload ? R.string.transfer_uploading_file : R.string.transfer_downloading_file, file); | |
90 handler.post(new Runnable() { | |
91 public void run() { | |
92 if (prefs.getBoolean(PreferenceConstants.BACKGROUND_FILE_TRANSFER, true)) { | |
93 if (progressToast == null) | |
94 progressToast = Toast.makeText(activity, newMessage, Toast.LENGTH_LONG); | |
95 else | |
96 progressToast.setText(newMessage); | |
97 | |
98 progressToast.show(); | |
99 } | |
100 else if (progress != null) { | |
101 progress.setMessage(newMessage); | |
102 } | |
103 } | |
104 }); | |
105 boolean success = (upload ? bridge.uploadFile(file, destName, destFolder, null) : bridge.downloadFile(file, destFolder)); | |
106 | |
107 if (! success) | |
108 fail += " " + file; | |
109 } | |
110 } | |
111 finally { | |
112 final String failMessage = (fail.length() == 0 ? null : res.getString(upload ? R.string.transfer_upload_failed : R.string.transfer_download_failed, fail)); | |
113 final String sucMessage = (res.getString(upload ? R.string.transfer_upload_complete : R.string.transfer_download_complete)); | |
114 handler.post(new Runnable() { | |
115 public void run() { | |
116 if (progress != null) | |
117 progress.dismiss(); | |
118 | |
119 if (prefs.getBoolean(PreferenceConstants.BACKGROUND_FILE_TRANSFER, true)) { | |
120 Toast.makeText(activity, | |
121 failMessage != null ? failMessage : sucMessage, | |
122 Toast.LENGTH_LONG) | |
123 .show(); | |
124 } | |
125 else if (failMessage != null) { | |
126 new AlertDialog.Builder(activity) | |
127 .setMessage(failMessage) | |
128 .setNegativeButton(android.R.string.ok, null).create().show(); | |
129 } | |
130 } | |
131 }); | |
132 } | |
133 } | |
134 | |
135 private void configureProgressDialog() { | |
136 if (dialogMessage != null) | |
137 progress = fileProgressDialog(activity, this.dialogMessage); | |
138 else | |
139 progress = null; | |
140 } | |
141 | |
142 private ProgressDialog fileProgressDialog(Activity activity, String message) { | |
143 ProgressDialog progress = new ProgressDialog(activity); | |
144 progress.setIndeterminate(true); | |
145 progress.setMessage(message); | |
146 progress.setCancelable(false); | |
147 progress.show(); | |
148 return progress; | |
149 } | |
150 } |