blob: cca26ccb75e300e821c895edf528111ba0a6f9fe [file] [log] [blame]
luxiaol62677b02013-07-22 07:54:49 +08001/*
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -07002 * Copyright (c) 2013,2014, The Linux Foundation. All rights reserved.
luxiaol62677b02013-07-22 07:54:49 +08003 *
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
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -070054import com.android.browser.reflect.ReflectHelper;
55
luxiaol62677b02013-07-22 07:54:49 +080056public class DownloadSettings extends Activity {
57
58 private EditText downloadFilenameET;
59 private EditText downloadPathET;
60 private TextView downloadEstimateSize;
61 private TextView downloadEstimateTime;
62 private Button downloadStart;
63 private Button downloadCancel;
64 private String url;
65 private String userAgent;
66 private String contentDisposition;
67 private String mimetype;
68 private String referer;
69 private String filenameBase;
70 private String filename;
71 private String filenameExtension;
72 private boolean privateBrowsing;
73 private long contentLength;
74 private String downloadPath;
75 private String downloadPathForUser;
76 private static final int downloadRate = (1024 * 100 * 60);// Download Rate
77 // 100KB/s
78 private final static String LOGTAG = "DownloadSettings";
79 private final static int DOWNLOAD_PATH = 0;
80 private boolean isDownloadStarted = false;
81
82 private static final String ENV_EMULATED_STORAGE_TARGET = "EMULATED_STORAGE_TARGET";
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -070083 private static final String APK_TYPE="apk";
84 private static final String OCTET_STREAM = "application/octet-stream";
luxiaol62677b02013-07-22 07:54:49 +080085
86 protected void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88 // initial the DownloadSettings view
89 requestWindowFeature(Window.FEATURE_NO_TITLE);
90 setContentView(R.layout.download_settings);
91 downloadFilenameET = (EditText) findViewById(R.id.download_filename_edit);
92 downloadPathET = (EditText) findViewById(R.id.download_filepath_selected);
93 downloadEstimateSize = (TextView) findViewById(R.id.download_estimate_size_content);
94 downloadEstimateTime = (TextView) findViewById(R.id.download_estimate_time_content);
95 downloadStart = (Button) findViewById(R.id.download_start);
96 downloadCancel = (Button) findViewById(R.id.download_cancle);
97 downloadPathET.setOnClickListener(downloadPathListener);
98 downloadStart.setOnClickListener(downloadStartListener);
99 downloadCancel.setOnClickListener(downloadCancelListener);
100
101 // get the bundle from Intent
102 Intent intent = getIntent();
103 Bundle fileInfo = intent.getExtras();
104 url = fileInfo.getString("url");
105 userAgent = fileInfo.getString("userAgent");
106 contentDisposition = fileInfo.getString("contentDisposition");
107 mimetype = fileInfo.getString("mimetype");
108 referer = fileInfo.getString("referer");
109 contentLength = fileInfo.getLong("contentLength");
110 privateBrowsing = fileInfo.getBoolean("privateBrowsing");
111 filename = fileInfo.getString("filename");
112
113 // download filenamebase's length is depended on filenameLength's values
114 // if filenamebase.length >= flienameLength, destroy the last string!
115
116 filenameBase = DownloadHandler.getFilenameBase(filename);
117 if (filenameBase.length() >= (BrowserUtils.FILENAME_MAX_LENGTH)) {
118 filenameBase = filenameBase.substring(0, BrowserUtils.FILENAME_MAX_LENGTH);
119 }
120
121 // warring when user enter more over letters into the EditText
122 BrowserUtils.maxLengthFilter(DownloadSettings.this, downloadFilenameET,
123 BrowserUtils.FILENAME_MAX_LENGTH);
124
125 downloadFilenameET.setText(filenameBase);
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -0700126
127 String filenameExtension = DownloadHandler.getFilenameExtension(filename);
128
129 if (mimetype == null || mimetype.isEmpty()) {
130
131 String updatedFileName = filenameBase + "." + filenameExtension;
132 Object[] params = {updatedFileName};
133 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -0700134 mimetype = (String) ReflectHelper.invokeMethod("android.media.MediaFile",
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -0700135 "getMimeTypeForFile", type, params);
136 }
137
138 //Add special check for .apk files with octet-stream mimetype
Axesh R. Ajmera77258982014-04-02 14:39:09 -0700139 if (filenameExtension.equals(APK_TYPE) && mimetype != null && mimetype.equals(OCTET_STREAM)) {
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -0700140 mimetype = "application/vnd.android.package-archive";
141 }
142
kaiyizf1a66762013-09-16 16:59:43 +0800143 downloadPath = chooseFolderFromMimeType(BrowserSettings.getInstance().getDownloadPath(),
144 mimetype);
luxiaol62677b02013-07-22 07:54:49 +0800145 downloadPathForUser = DownloadHandler.getDownloadPathForUser(DownloadSettings.this,
146 downloadPath);
147 setDownloadPathForUserText(downloadPathForUser);
148 setDownloadFileSizeText();
149 setDownloadFileTimeText();
luxiaol62677b02013-07-22 07:54:49 +0800150 }
151
152 private OnClickListener downloadPathListener = new OnClickListener() {
153
154 @Override
155 public void onClick(View v) {
156
157 // start filemanager for getting download path
158 try {
159 Intent downloadPathIntent = new Intent("com.android.fileexplorer.action.DIR_SEL");
160 DownloadSettings.this.startActivityForResult(downloadPathIntent, DOWNLOAD_PATH);
161 } catch (Exception e) {
162 String err_msg = getString(R.string.activity_not_found,
163 "com.android.fileexplorer.action.DIR_SEL");
164 Toast.makeText(DownloadSettings.this, err_msg, Toast.LENGTH_LONG).show();
165 }
166
167 }
168 };
169
170 private OnClickListener downloadStartListener = new OnClickListener() {
171
172 @Override
173 public void onClick(View v) {
174 filenameBase = getFilenameBaseFromUserEnter();
175 // check the filename user enter is null or not
176 if (filenameBase.length() <= 0) {
177 DownloadHandler.showFilenameEmptyDialog(DownloadSettings.this);
178 return;
179 }
180
181 filenameExtension = DownloadHandler.getFilenameExtension(filename);
182 filename = filenameBase + "." + filenameExtension;
183
184 // check the storage status
185 if (!DownloadHandler.isStorageStatusOK(DownloadSettings.this, filename, downloadPath)) {
186 return;
187 }
188
189 // check the storage memory enough or not
190 try {
191 DownloadHandler.setAppointedFolder(downloadPath);
192 } catch (Exception e) {
193 DownloadHandler.showNoEnoughMemoryDialog(DownloadSettings.this);
194 return;
195 }
kaiyize6a27d02013-08-22 15:08:19 +0800196 boolean isNoEnoughMemory = DownloadHandler.manageNoEnoughMemory(contentLength,
197 downloadPath);
luxiaol62677b02013-07-22 07:54:49 +0800198 if (isNoEnoughMemory) {
kaiyize6a27d02013-08-22 15:08:19 +0800199 DownloadHandler.showNoEnoughMemoryDialog(DownloadSettings.this);
luxiaol62677b02013-07-22 07:54:49 +0800200 return;
201 }
202
203 // check the download file is exist or not
204 String fullFilename = downloadPath + "/" + filename;
205 if (mimetype != null && new File(fullFilename).exists()) {
206 DownloadHandler.fileExistQueryDialog(DownloadSettings.this);
207 return;
208 }
209
210 // staring downloading
211 DownloadHandler.startingDownload(DownloadSettings.this,
212 url, userAgent, contentDisposition,
213 mimetype, referer, privateBrowsing, contentLength,
214 Uri.encode(filename), downloadPath);
215 isDownloadStarted = true;
216 }
217 };
218
219 private OnClickListener downloadCancelListener = new OnClickListener() {
luxiaol62677b02013-07-22 07:54:49 +0800220 @Override
221 public void onClick(View v) {
222 finish();
223 }
224 };
225
226 protected void onDestroy() {
227 super.onDestroy();
228 }
229
230 protected void onPause() {
231 super.onPause();
232 if (isDownloadStarted) {
233 finish();
234 }
235 }
236
237 protected void onResume() {
238 super.onResume();
239 }
240
241 @Override
242 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
243
244 if (DOWNLOAD_PATH == requestCode) {
245 if (resultCode == Activity.RESULT_OK && intent != null) {
246 downloadPath = intent.getStringExtra("result_dir_sel");
247 if (downloadPath != null) {
248 String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
249 if (!TextUtils.isEmpty(rawEmulatedStorageTarget)) {
250 if (downloadPath.startsWith("/storage/sdcard0"))
251 downloadPath = downloadPath.replace("/storage/sdcard0",
252 "/storage/emulated/0");
253 if (downloadPath.startsWith("/storage/emulated/legacy"))
254 downloadPath = downloadPath.replace("/storage/emulated/legacy",
255 "/storage/emulated/0");
256 }
257 downloadPathForUser = DownloadHandler.getDownloadPathForUser(
258 DownloadSettings.this, downloadPath);
259 setDownloadPathForUserText(downloadPathForUser);
260 }
261 }
262 }
263 }
264
kaiyizf1a66762013-09-16 16:59:43 +0800265 // Add for carrier feature - download to related folders by mimetype.
266 private static String chooseFolderFromMimeType(String path, String mimeType) {
267 String destinationFolder = null;
268 if (!path.contains(Environment.DIRECTORY_DOWNLOADS) || null == mimeType)
269 return path;
270 if (mimeType.startsWith("audio"))
271 destinationFolder = Environment.DIRECTORY_MUSIC;
272 else if (mimeType.startsWith("video"))
273 destinationFolder = Environment.DIRECTORY_MOVIES;
274 else if (mimeType.startsWith("image"))
275 destinationFolder = Environment.DIRECTORY_PICTURES;
276 if (null != destinationFolder)
277 path = path.replace(Environment.DIRECTORY_DOWNLOADS, destinationFolder);
278 return path;
279 }
280
luxiaol62677b02013-07-22 07:54:49 +0800281 /**
282 * show download path for user
283 *
284 * @param downloadPath the download path user can see
285 */
286 private void setDownloadPathForUserText(String downloadPathForUser) {
287 downloadPathET.setText(downloadPathForUser);
288 }
289
290 /**
291 * get the filename from user select the download path
292 *
293 * @return String the filename from user selected
294 */
295 private String getFilenameBaseFromUserEnter() {
296 return downloadFilenameET.getText().toString();
297 }
298
299 /**
300 * set the download file size for user to be known
301 */
302 private void setDownloadFileSizeText() {
303 String sizeText;
304 if (contentLength <= 0) {
305 sizeText = getString(R.string.unknow_length);
306 } else {
307 sizeText = getDownloadFileSize();
308 }
309 downloadEstimateSize.setText(sizeText);
310
311 }
312
313 /**
314 * set the time which downloaded this file will be estimately use;
315 */
316 private void setDownloadFileTimeText() {
317 String neededTimeText;
318 if (contentLength <= 0) {
319 neededTimeText = getString(R.string.unknow_length);
320 } else {
321 neededTimeText = getNeededTime() + getString(R.string.time_min);
322 }
323 downloadEstimateTime.setText(neededTimeText);
324 }
325
326 /**
327 * count the download file's size and format the values
328 *
329 * @return String the format values
330 */
331 private String getDownloadFileSize() {
332 String currentSizeText = "";
333 if (contentLength > 0) {
334 currentSizeText = Formatter.formatFileSize(DownloadSettings.this, contentLength);
335 }
336 return currentSizeText;
337 }
338
339 /**
340 * get the time download this file will be use,and format this time values
341 *
342 * @return long the valses of time which download this file will be use
343 */
344 private long getNeededTime() {
345 long timeNeeded = contentLength / downloadRate;
346 if (timeNeeded < 1) {
347 timeNeeded = 1;
348 }
349 Log.e(LOGTAG, "TimeNeeded:" + timeNeeded + "min");
350 // return the time like 5 min, not 5 s;
351 return timeNeeded;
352 }
353}