blob: 698fa88632d8dd31b9fa3d07f4119e746a2f48e7 [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;
kaiyizf1a66762013-09-16 16:59:43 +080039import android.os.Environment;
luxiaol62677b02013-07-22 07:54:49 +080040import android.text.format.*;
41import android.util.Log;
42import android.view.View;
43import android.view.View.OnClickListener;
44import android.widget.Button;
45import android.widget.EditText;
46import android.widget.TextView;
47import android.view.Window;
48import android.widget.Toast;
49import android.text.TextUtils;
50
51public class DownloadSettings extends Activity {
52
53 private EditText downloadFilenameET;
54 private EditText downloadPathET;
55 private TextView downloadEstimateSize;
56 private TextView downloadEstimateTime;
57 private Button downloadStart;
58 private Button downloadCancel;
59 private String url;
60 private String userAgent;
61 private String contentDisposition;
62 private String mimetype;
63 private String referer;
64 private String filenameBase;
65 private String filename;
66 private String filenameExtension;
67 private boolean privateBrowsing;
68 private long contentLength;
69 private String downloadPath;
70 private String downloadPathForUser;
71 private static final int downloadRate = (1024 * 100 * 60);// Download Rate
72 // 100KB/s
73 private final static String LOGTAG = "DownloadSettings";
74 private final static int DOWNLOAD_PATH = 0;
75 private boolean isDownloadStarted = false;
76
77 private static final String ENV_EMULATED_STORAGE_TARGET = "EMULATED_STORAGE_TARGET";
78
79 protected void onCreate(Bundle savedInstanceState) {
80 super.onCreate(savedInstanceState);
81 // initial the DownloadSettings view
82 requestWindowFeature(Window.FEATURE_NO_TITLE);
83 setContentView(R.layout.download_settings);
84 downloadFilenameET = (EditText) findViewById(R.id.download_filename_edit);
85 downloadPathET = (EditText) findViewById(R.id.download_filepath_selected);
86 downloadEstimateSize = (TextView) findViewById(R.id.download_estimate_size_content);
87 downloadEstimateTime = (TextView) findViewById(R.id.download_estimate_time_content);
88 downloadStart = (Button) findViewById(R.id.download_start);
89 downloadCancel = (Button) findViewById(R.id.download_cancle);
90 downloadPathET.setOnClickListener(downloadPathListener);
91 downloadStart.setOnClickListener(downloadStartListener);
92 downloadCancel.setOnClickListener(downloadCancelListener);
93
94 // get the bundle from Intent
95 Intent intent = getIntent();
96 Bundle fileInfo = intent.getExtras();
97 url = fileInfo.getString("url");
98 userAgent = fileInfo.getString("userAgent");
99 contentDisposition = fileInfo.getString("contentDisposition");
100 mimetype = fileInfo.getString("mimetype");
101 referer = fileInfo.getString("referer");
102 contentLength = fileInfo.getLong("contentLength");
103 privateBrowsing = fileInfo.getBoolean("privateBrowsing");
104 filename = fileInfo.getString("filename");
105
106 // download filenamebase's length is depended on filenameLength's values
107 // if filenamebase.length >= flienameLength, destroy the last string!
108
109 filenameBase = DownloadHandler.getFilenameBase(filename);
110 if (filenameBase.length() >= (BrowserUtils.FILENAME_MAX_LENGTH)) {
111 filenameBase = filenameBase.substring(0, BrowserUtils.FILENAME_MAX_LENGTH);
112 }
113
114 // warring when user enter more over letters into the EditText
115 BrowserUtils.maxLengthFilter(DownloadSettings.this, downloadFilenameET,
116 BrowserUtils.FILENAME_MAX_LENGTH);
117
118 downloadFilenameET.setText(filenameBase);
kaiyizf1a66762013-09-16 16:59:43 +0800119 downloadPath = chooseFolderFromMimeType(BrowserSettings.getInstance().getDownloadPath(),
120 mimetype);
luxiaol62677b02013-07-22 07:54:49 +0800121 downloadPathForUser = DownloadHandler.getDownloadPathForUser(DownloadSettings.this,
122 downloadPath);
123 setDownloadPathForUserText(downloadPathForUser);
124 setDownloadFileSizeText();
125 setDownloadFileTimeText();
126
127 }
128
129 private OnClickListener downloadPathListener = new OnClickListener() {
130
131 @Override
132 public void onClick(View v) {
133
134 // start filemanager for getting download path
135 try {
136 Intent downloadPathIntent = new Intent("com.android.fileexplorer.action.DIR_SEL");
137 DownloadSettings.this.startActivityForResult(downloadPathIntent, DOWNLOAD_PATH);
138 } catch (Exception e) {
139 String err_msg = getString(R.string.activity_not_found,
140 "com.android.fileexplorer.action.DIR_SEL");
141 Toast.makeText(DownloadSettings.this, err_msg, Toast.LENGTH_LONG).show();
142 }
143
144 }
145 };
146
147 private OnClickListener downloadStartListener = new OnClickListener() {
148
149 @Override
150 public void onClick(View v) {
151 filenameBase = getFilenameBaseFromUserEnter();
152 // check the filename user enter is null or not
153 if (filenameBase.length() <= 0) {
154 DownloadHandler.showFilenameEmptyDialog(DownloadSettings.this);
155 return;
156 }
157
158 filenameExtension = DownloadHandler.getFilenameExtension(filename);
159 filename = filenameBase + "." + filenameExtension;
160
161 // check the storage status
162 if (!DownloadHandler.isStorageStatusOK(DownloadSettings.this, filename, downloadPath)) {
163 return;
164 }
165
166 // check the storage memory enough or not
167 try {
168 DownloadHandler.setAppointedFolder(downloadPath);
169 } catch (Exception e) {
170 DownloadHandler.showNoEnoughMemoryDialog(DownloadSettings.this);
171 return;
172 }
kaiyize6a27d02013-08-22 15:08:19 +0800173 boolean isNoEnoughMemory = DownloadHandler.manageNoEnoughMemory(contentLength,
174 downloadPath);
luxiaol62677b02013-07-22 07:54:49 +0800175 if (isNoEnoughMemory) {
kaiyize6a27d02013-08-22 15:08:19 +0800176 DownloadHandler.showNoEnoughMemoryDialog(DownloadSettings.this);
luxiaol62677b02013-07-22 07:54:49 +0800177 return;
178 }
179
180 // check the download file is exist or not
181 String fullFilename = downloadPath + "/" + filename;
182 if (mimetype != null && new File(fullFilename).exists()) {
183 DownloadHandler.fileExistQueryDialog(DownloadSettings.this);
184 return;
185 }
186
187 // staring downloading
188 DownloadHandler.startingDownload(DownloadSettings.this,
189 url, userAgent, contentDisposition,
190 mimetype, referer, privateBrowsing, contentLength,
191 Uri.encode(filename), downloadPath);
192 isDownloadStarted = true;
193 }
194 };
195
196 private OnClickListener downloadCancelListener = new OnClickListener() {
197
198 @Override
199 public void onClick(View v) {
200 finish();
201 }
202 };
203
204 protected void onDestroy() {
205 super.onDestroy();
206 }
207
208 protected void onPause() {
209 super.onPause();
210 if (isDownloadStarted) {
211 finish();
212 }
213 }
214
215 protected void onResume() {
216 super.onResume();
217 }
218
219 @Override
220 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
221
222 if (DOWNLOAD_PATH == requestCode) {
223 if (resultCode == Activity.RESULT_OK && intent != null) {
224 downloadPath = intent.getStringExtra("result_dir_sel");
225 if (downloadPath != null) {
226 String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
227 if (!TextUtils.isEmpty(rawEmulatedStorageTarget)) {
228 if (downloadPath.startsWith("/storage/sdcard0"))
229 downloadPath = downloadPath.replace("/storage/sdcard0",
230 "/storage/emulated/0");
231 if (downloadPath.startsWith("/storage/emulated/legacy"))
232 downloadPath = downloadPath.replace("/storage/emulated/legacy",
233 "/storage/emulated/0");
234 }
235 downloadPathForUser = DownloadHandler.getDownloadPathForUser(
236 DownloadSettings.this, downloadPath);
237 setDownloadPathForUserText(downloadPathForUser);
238 }
239 }
240 }
241 }
242
kaiyizf1a66762013-09-16 16:59:43 +0800243 // Add for carrier feature - download to related folders by mimetype.
244 private static String chooseFolderFromMimeType(String path, String mimeType) {
245 String destinationFolder = null;
246 if (!path.contains(Environment.DIRECTORY_DOWNLOADS) || null == mimeType)
247 return path;
248 if (mimeType.startsWith("audio"))
249 destinationFolder = Environment.DIRECTORY_MUSIC;
250 else if (mimeType.startsWith("video"))
251 destinationFolder = Environment.DIRECTORY_MOVIES;
252 else if (mimeType.startsWith("image"))
253 destinationFolder = Environment.DIRECTORY_PICTURES;
254 if (null != destinationFolder)
255 path = path.replace(Environment.DIRECTORY_DOWNLOADS, destinationFolder);
256 return path;
257 }
258
luxiaol62677b02013-07-22 07:54:49 +0800259 /**
260 * show download path for user
261 *
262 * @param downloadPath the download path user can see
263 */
264 private void setDownloadPathForUserText(String downloadPathForUser) {
265 downloadPathET.setText(downloadPathForUser);
266 }
267
268 /**
269 * get the filename from user select the download path
270 *
271 * @return String the filename from user selected
272 */
273 private String getFilenameBaseFromUserEnter() {
274 return downloadFilenameET.getText().toString();
275 }
276
277 /**
278 * set the download file size for user to be known
279 */
280 private void setDownloadFileSizeText() {
281 String sizeText;
282 if (contentLength <= 0) {
283 sizeText = getString(R.string.unknow_length);
284 } else {
285 sizeText = getDownloadFileSize();
286 }
287 downloadEstimateSize.setText(sizeText);
288
289 }
290
291 /**
292 * set the time which downloaded this file will be estimately use;
293 */
294 private void setDownloadFileTimeText() {
295 String neededTimeText;
296 if (contentLength <= 0) {
297 neededTimeText = getString(R.string.unknow_length);
298 } else {
299 neededTimeText = getNeededTime() + getString(R.string.time_min);
300 }
301 downloadEstimateTime.setText(neededTimeText);
302 }
303
304 /**
305 * count the download file's size and format the values
306 *
307 * @return String the format values
308 */
309 private String getDownloadFileSize() {
310 String currentSizeText = "";
311 if (contentLength > 0) {
312 currentSizeText = Formatter.formatFileSize(DownloadSettings.this, contentLength);
313 }
314 return currentSizeText;
315 }
316
317 /**
318 * get the time download this file will be use,and format this time values
319 *
320 * @return long the valses of time which download this file will be use
321 */
322 private long getNeededTime() {
323 long timeNeeded = contentLength / downloadRate;
324 if (timeNeeded < 1) {
325 timeNeeded = 1;
326 }
327 Log.e(LOGTAG, "TimeNeeded:" + timeNeeded + "min");
328 // return the time like 5 min, not 5 s;
329 return timeNeeded;
330 }
331}