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