blob: c1ddcb6790baa04e5b69bd367af8e9259d0acf25 [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 }
171 boolean isNoEnoughMemory = DownloadHandler.manageNoEnoughMemory(DownloadSettings.this,
172 contentLength, downloadPath);
173 if (isNoEnoughMemory) {
174 return;
175 }
176
177 // check the download file is exist or not
178 String fullFilename = downloadPath + "/" + filename;
179 if (mimetype != null && new File(fullFilename).exists()) {
180 DownloadHandler.fileExistQueryDialog(DownloadSettings.this);
181 return;
182 }
183
184 // staring downloading
185 DownloadHandler.startingDownload(DownloadSettings.this,
186 url, userAgent, contentDisposition,
187 mimetype, referer, privateBrowsing, contentLength,
188 Uri.encode(filename), downloadPath);
189 isDownloadStarted = true;
190 }
191 };
192
193 private OnClickListener downloadCancelListener = new OnClickListener() {
194
195 @Override
196 public void onClick(View v) {
197 finish();
198 }
199 };
200
201 protected void onDestroy() {
202 super.onDestroy();
203 }
204
205 protected void onPause() {
206 super.onPause();
207 if (isDownloadStarted) {
208 finish();
209 }
210 }
211
212 protected void onResume() {
213 super.onResume();
214 }
215
216 @Override
217 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
218
219 if (DOWNLOAD_PATH == requestCode) {
220 if (resultCode == Activity.RESULT_OK && intent != null) {
221 downloadPath = intent.getStringExtra("result_dir_sel");
222 if (downloadPath != null) {
223 String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
224 if (!TextUtils.isEmpty(rawEmulatedStorageTarget)) {
225 if (downloadPath.startsWith("/storage/sdcard0"))
226 downloadPath = downloadPath.replace("/storage/sdcard0",
227 "/storage/emulated/0");
228 if (downloadPath.startsWith("/storage/emulated/legacy"))
229 downloadPath = downloadPath.replace("/storage/emulated/legacy",
230 "/storage/emulated/0");
231 }
232 downloadPathForUser = DownloadHandler.getDownloadPathForUser(
233 DownloadSettings.this, downloadPath);
234 setDownloadPathForUserText(downloadPathForUser);
235 }
236 }
237 }
238 }
239
240 /**
241 * show download path for user
242 *
243 * @param downloadPath the download path user can see
244 */
245 private void setDownloadPathForUserText(String downloadPathForUser) {
246 downloadPathET.setText(downloadPathForUser);
247 }
248
249 /**
250 * get the filename from user select the download path
251 *
252 * @return String the filename from user selected
253 */
254 private String getFilenameBaseFromUserEnter() {
255 return downloadFilenameET.getText().toString();
256 }
257
258 /**
259 * set the download file size for user to be known
260 */
261 private void setDownloadFileSizeText() {
262 String sizeText;
263 if (contentLength <= 0) {
264 sizeText = getString(R.string.unknow_length);
265 } else {
266 sizeText = getDownloadFileSize();
267 }
268 downloadEstimateSize.setText(sizeText);
269
270 }
271
272 /**
273 * set the time which downloaded this file will be estimately use;
274 */
275 private void setDownloadFileTimeText() {
276 String neededTimeText;
277 if (contentLength <= 0) {
278 neededTimeText = getString(R.string.unknow_length);
279 } else {
280 neededTimeText = getNeededTime() + getString(R.string.time_min);
281 }
282 downloadEstimateTime.setText(neededTimeText);
283 }
284
285 /**
286 * count the download file's size and format the values
287 *
288 * @return String the format values
289 */
290 private String getDownloadFileSize() {
291 String currentSizeText = "";
292 if (contentLength > 0) {
293 currentSizeText = Formatter.formatFileSize(DownloadSettings.this, contentLength);
294 }
295 return currentSizeText;
296 }
297
298 /**
299 * get the time download this file will be use,and format this time values
300 *
301 * @return long the valses of time which download this file will be use
302 */
303 private long getNeededTime() {
304 long timeNeeded = contentLength / downloadRate;
305 if (timeNeeded < 1) {
306 timeNeeded = 1;
307 }
308 Log.e(LOGTAG, "TimeNeeded:" + timeNeeded + "min");
309 // return the time like 5 min, not 5 s;
310 return timeNeeded;
311 }
312}