admin
2024-01-26 c2d382d99ca506932985d1843d4371d6ed0203ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.mozillaonline.downloadprovider;
 
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 
import com.mozillaonline.providers.DownloadManager;
import com.mozillaonline.providers.DownloadManager.Request;
import com.mozillaonline.providers.downloads.DownloadService;
import com.mozillaonline.providers.downloads.ui.DownloadList;
 
public class DownloadProviderActivity extends Activity implements
    OnClickListener {
    @SuppressWarnings("unused")
    private static final String TAG = DownloadProviderActivity.class.getName();
 
    private BroadcastReceiver mReceiver;
 
    EditText mUrlInputEditText;
    Button mStartDownloadButton;
    DownloadManager mDownloadManager;
    Button mShowDownloadListButton;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 
    mDownloadManager = new DownloadManager(getContentResolver(),
        getPackageName());
    buildComponents();
    startDownloadService();
 
    mReceiver = new BroadcastReceiver() {
 
        @Override
        public void onReceive(Context context, Intent intent) {
        showDownloadList();
        }
    };
 
    registerReceiver(mReceiver, new IntentFilter(
        DownloadManager.ACTION_NOTIFICATION_CLICKED));
    }
 
    @Override
    protected void onDestroy() {
    unregisterReceiver(mReceiver);
    super.onDestroy();
    }
 
    private void buildComponents() {
    mUrlInputEditText = (EditText) findViewById(R.id.url_input_edittext);
    mStartDownloadButton = (Button) findViewById(R.id.start_download_button);
    mShowDownloadListButton = (Button) findViewById(R.id.show_download_list_button);
 
    mStartDownloadButton.setOnClickListener(this);
    mShowDownloadListButton.setOnClickListener(this);
 
    mUrlInputEditText.setText("http://down.mumayi.com/41052/mbaidu");
    }
 
    private void startDownloadService() {
    Intent intent = new Intent();
    intent.setClass(this, DownloadService.class);
    startService(intent);
    }
 
    @Override
    public void onClick(View v) {
        if (v.getId() == getResources().getIdentifier("start_download_button",
                "id", getPackageName())) {
            startDownload();
        } else if (v.getId() == getResources().getIdentifier("show_download_list_button",
                "id", getPackageName())) {
            showDownloadList();
        }
    }
 
    private void showDownloadList() {
    Intent intent = new Intent();
    intent.setClass(this, DownloadList.class);
    startActivity(intent);
    }
 
    private void startDownload() {
    String url = mUrlInputEditText.getText().toString();
    Uri srcUri = Uri.parse(url);
    DownloadManager.Request request = new Request(srcUri);
    request.setDestinationInExternalPublicDir(
        Environment.DIRECTORY_DOWNLOADS, "/");
    request.setDescription("Just for test");
    mDownloadManager.enqueue(request);
    }
}