package com.chiclam.download;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.et_download);

        //如果没有停用,先去停用,然后点击下载按钮. 测试用
        showDownloadSetting();
    }

    private void showDownloadSetting() {
        String packageName = "com.android.providers.downloads";
        Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + packageName));
        if (intentAvailable(intent)) {
            startActivity(intent);
        }
    }

    private boolean intentAvailable(Intent intent) {
        PackageManager packageManager = getPackageManager();
        List list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }


    private boolean canDownloadState() {
        try {
            int state = this.getPackageManager().getApplicationEnabledSetting("com.android.providers.downloads");

            if (state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                    || state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER
                    || state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public void download(View view) {
        if (!canDownloadState()) {
            Toast.makeText(this, "下载服务不用,请您启用", Toast.LENGTH_SHORT).show();
            showDownloadSetting();
            return;
        }
        String url;
        if (TextUtils.isEmpty(editText.getText().toString())) {
            url = "http://www.hoolay.cn";
        } else {
            url = editText.getText().toString();
        }
        ApkUpdateUtils.download(this, url, getResources().getString(R.string.app_name));
    }

}

@@ -643,16 +643,86 @@
 nload);%0A
+        editText.setText(%22http://releases.b0.upaiyun.com/hoolay.apk%22);
 %0A       
@@ -749,16 +749,23 @@
 %E8%BD%BD%E6%8C%89%E9%92%AE. %E6%B5%8B%E8%AF%95%E7%94%A8
+%E6%88%B7%E5%85%B3%E9%97%AD%E4%B8%8B%E8%BD%BD%E6%9C%8D%E5%8A%A1
 %0A       
@@ -757,32 +757,34 @@
 %E6%88%B7%E5%85%B3%E9%97%AD%E4%B8%8B%E8%BD%BD%E6%9C%8D%E5%8A%A1%0A        
+//
 showDownloadSett
@@ -2332,21 +2332,42 @@
 p://
-www.
+releases.b0.upaiyun.com/
 hoolay.
-cn
+apk
 %22;%0A 

add apk url