blob: aecb5436d7e4b6d51b33a8c39d9f1ea5711682a3 [file] [log] [blame]
luxiaol62677b02013-07-22 07:54:49 +08001/*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package com.android.browser;
31
32import java.io.File;
33
34import android.app.Activity;
35import android.content.Intent;
36import java.lang.Thread;
37import android.net.Uri;
38import android.os.Bundle;
39import android.text.format.*;
40import android.util.Log;
41import android.view.View;
42import android.view.View.OnClickListener;
43import android.widget.Button;
44import android.widget.EditText;
45import android.widget.TextView;
46import android.view.Window;
47import android.widget.Toast;
48import android.text.TextUtils;
49
50public class DownloadSettings extends Activity {
51
52 private EditText downloadFilenameET;
53 private EditText downloadPathET;
54 private TextView downloadEstimateSize;
55 private TextView downloadEstimateTime;
56 private Button downloadStart;
57 private Button downloadCancel;
58 private String url;
59 private String userAgent;
60 private String contentDisposition;
61 private String mimetype;
62 private String referer;
63 private String filenameBase;
64 private String filename;
65 private String filenameExtension;
66 private boolean privateBrowsing;
67 private long contentLength;
68 private String downloadPath;
69 private String downloadPathForUser;
70 private static final int downloadRate = (1024 * 100 * 60);// Download Rate
71 // 100KB/s
72 private final static String LOGTAG = "DownloadSettings";
73 private final static int DOWNLOAD_PATH = 0;
74 private boolean isDownloadStarted = false;
75
76 private static final String ENV_EMULATED_STORAGE_TARGET = "EMULATED_STORAGE_TARGET";
77
78 protected void onCreate(Bundle savedInstanceState) {
79 super.onCreate(savedInstanceState);
80 // initial the DownloadSettings view
81 requestWindowFeature(Window.FEATURE_NO_TITLE);
82 setContentView(R.layout.download_settings);
83 downloadFilenameET = (EditText) findViewById(R.id.download_filename_edit);
84 downloadPathET = (EditText) findViewById(R.id.download_filepath_selected);
85 downloadEstimateSize = (TextView) findViewById(R.id.download_estimate_size_content);
86 downloadEstimateTime = (TextView) findViewById(R.id.download_estimate_time_content);
87 downloadStart = (Button) findViewById(R.id.download_start);
88 downloadCancel = (Button) findViewById(R.id.download_cancle);
89 downloadPathET.setOnClickListener(downloadPathListener);
90 downloadStart.setOnClickListener(downloadStartListener);
91 downloadCancel.setOnClickListener(downloadCancelListener);
92
93 // get the bundle from Intent
94 Intent intent = getIntent();
95 Bundle fileInfo = intent.getExtras();
96 url = fileInfo.getString("url");
97 userAgent = fileInfo.getString("userAgent");
98 contentDisposition = fileInfo.getString("contentDisposition");
99 mimetype = fileInfo.getString("mimetype");
100 referer = fileInfo.getString("referer");
101 contentLength = fileInfo.getLong("contentLength");
102 privateBrowsing = fileInfo.getBoolean("privateBrowsing");
103 filename = fileInfo.getString("filename");
104
105 // download filenamebase's length is depended on filenameLength's values
106 // if filenamebase.length >= flienameLength, destroy the last string!
107
108 filenameBase = DownloadHandler.getFilenameBase(filename);
109 if (filenameBase.length() >= (BrowserUtils.FILENAME_MAX_LENGTH)) {
110 filenameBase = filenameBase.substring(0, BrowserUtils.FILENAME_MAX_LENGTH);
111 }
112
113 // warring when user enter more over letters into the EditText
114 BrowserUtils.maxLengthFilter(DownloadSettings.this, downloadFilenameET,
115 BrowserUtils.FILENAME_MAX_LENGTH);
116
117 downloadFilenameET.setText(filenameBase);
118 downloadPath = BrowserSettings.getInstance().getDownloadPath();
119 downloadPathForUser = DownloadHandler.getDownloadPathForUser(DownloadSettings.this,
120 downloadPath);
121 setDownloadPathForUserText(downloadPathForUser);
122 setDownloadFileSizeText();
123 setDownloadFileTimeText();
124
125 }
126
127 private OnClickListener downloadPathListener = new OnClickListener() {
128
129 @Override
130 public void onClick(View v) {
131
132 // start filemanager for getting download path
133 try {
134 Intent downloadPathIntent = new Intent("com.android.fileexplorer.action.DIR_SEL");
135 DownloadSettings.this.startActivityForResult(downloadPathIntent, DOWNLOAD_PATH);
136 } catch (Exception e) {
137 String err_msg = getString(R.string.activity_not_found,
138 "com.android.fileexplorer.action.DIR_SEL");
139 Toast.makeText(DownloadSettings.this, err_msg, Toast.LENGTH_LONG).show();
140 }
141
142 }
143 };
144
145 private OnClickListener downloadStartListener = new OnClickListener() {
146
147 @Override
148 public void onClick(View v) {
149 filenameBase = getFilenameBaseFromUserEnter();
150 // check the filename user enter is null or not
151 if (filenameBase.length() <= 0) {
152 DownloadHandler.showFilenameEmptyDialog(DownloadSettings.this);
153 return;
154 }
155
156 filenameExtension = DownloadHandler.getFilenameExtension(filename);
157 filename = filenameBase + "." + filenameExtension;
158
159 // check the storage status
160 if (!DownloadHandler.isStorageStatusOK(DownloadSettings.this, filename, downloadPath)) {
161 return;
162 }
163
164 // check the storage memory enough or not
165 try {
166 DownloadHandler.setAppointedFolder(downloadPath);
167 } catch (Exception e) {
168 DownloadHandler.showNoEnoughMemoryDialog(DownloadSettings.this);
169 return;
170 }
kaiyize6a27d02013-08-22 15:08:19 +0800171 boolean isNoEnoughMemory = DownloadHandler.manageNoEnoughMemory(contentLength,
172 downloadPath);
luxiaol62677b02013-07-22 07:54:49 +0800173 if (isNoEnoughMemory) {
kaiyize6a27d02013-08-22 15:08:19 +0800174 DownloadHandler.showNoEnoughMemoryDialog(DownloadSettings.this);
luxiaol62677b02013-07-22 07:54:49 +0800175 return;
176 }
177
178 // check the download file is exist or not
179 String fullFilename = downloadPath + "/" + filename;
180 if (mimetype != null && new File(fullFilename).exists()) {
181 DownloadHandler.fileExistQueryDialog(DownloadSettings.this);
182 return;
183 }
184
185 // staring downloading
186 DownloadHandler.startingDownload(DownloadSettings.this,
187 url, userAgent, contentDisposition,
188 mimetype, referer, privateBrowsing, contentLength,
189 Uri.encode(filename), downloadPath);
190 isDownloadStarted = true;
191 }
192 };
193
194 private OnClickListener downloadCancelListener = new OnClickListener() {
195
196 @Override
197 public void onClick(View v) {
198 finish();
199 }
200 };
201
202 protected void onDestroy() {
203 super.onDestroy();
204 }
205
206 protected void onPause() {
207 super.onPause();
208 if (isDownloadStarted) {
209 finish();
210 }
211 }
212
213 protected void onResume() {
214 super.onResume();
215 }
216
217 @Override
218 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
219
220 if (DOWNLOAD_PATH == requestCode) {
221 if (resultCode == Activity.RESULT_OK && intent != null) {
222 downloadPath = intent.getStringExtra("result_dir_sel");
223 if (downloadPath != null) {
224 String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
225 if (!TextUtils.isEmpty(rawEmulatedStorageTarget)) {
226 if (downloadPath.startsWith("/storage/sdcard0"))
227 downloadPath = downloadPath.replace("/storage/sdcard0",
228 "/storage/emulated/0");
229 if (downloadPath.startsWith("/storage/emulated/legacy"))
230 downloadPath = downloadPath.replace("/storage/emulated/legacy",
231 "/storage/emulated/0");
232 }
233 downloadPathForUser = DownloadHandler.getDownloadPathForUser(
234 DownloadSettings.this, downloadPath);
235 setDownloadPathForUserText(downloadPathForUser);
236 }
237 }
238 }
239 }
240
241 /**
242 * show download path for user
243 *
244 * @param downloadPath the download path user can see
245 */
246 private void setDownloadPathForUserText(String downloadPathForUser) {
247 downloadPathET.setText(downloadPathForUser);
248 }
249
250 /**
251 * get the filename from user select the download path
252 *
253 * @return String the filename from user selected
254 */
255 private String getFilenameBaseFromUserEnter() {
256 return downloadFilenameET.getText().toString();
257 }
258
259 /**
260 * set the download file size for user to be known
261 */
262 private void setDownloadFileSizeText() {
263 String sizeText;
264 if (contentLength <= 0) {
265 sizeText = getString(R.string.unknow_length);
266 } else {
267 sizeText = getDownloadFileSize();
268 }
269 downloadEstimateSize.setText(sizeText);
270
271 }
272
273 /**
274 * set the time which downloaded this file will be estimately use;
275 */
276 private void setDownloadFileTimeText() {
277 String neededTimeText;
278 if (contentLength <= 0) {
279 neededTimeText = getString(R.string.unknow_length);
280 } else {
281 neededTimeText = getNeededTime() + getString(R.string.time_min);
282 }
283 downloadEstimateTime.setText(neededTimeText);
284 }
285
286 /**
287 * count the download file's size and format the values
288 *
289 * @return String the format values
290 */
291 private String getDownloadFileSize() {
292 String currentSizeText = "";
293 if (contentLength > 0) {
294 currentSizeText = Formatter.formatFileSize(DownloadSettings.this, contentLength);
295 }
296 return currentSizeText;
297 }
298
299 /**
300 * get the time download this file will be use,and format this time values
301 *
302 * @return long the valses of time which download this file will be use
303 */
304 private long getNeededTime() {
305 long timeNeeded = contentLength / downloadRate;
306 if (timeNeeded < 1) {
307 timeNeeded = 1;
308 }
309 Log.e(LOGTAG, "TimeNeeded:" + timeNeeded + "min");
310 // return the time like 5 min, not 5 s;
311 return timeNeeded;
312 }
313}