package cl.monsoon.s1next.view.activity;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

import cl.monsoon.s1next.R;
import cl.monsoon.s1next.databinding.ActivityGalleryBinding;
import cl.monsoon.s1next.util.IntentUtil;
import cl.monsoon.s1next.view.internal.ToolbarDelegate;
import cl.monsoon.s1next.viewmodel.ImageViewModel;

/**
 * An Activity shows an ImageView that supports multi-touch.
 */
public final class GalleryActivity extends AppCompatActivity {

    private static final String ARG_IMAGE_URL = "image_url";

    private String mImageUrl;

    public static void startGalleryActivity(Context context, String imageUrl) {
        Intent intent = new Intent(context, GalleryActivity.class);
        intent.putExtra(GalleryActivity.ARG_IMAGE_URL, imageUrl);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityGalleryBinding binding = DataBindingUtil.setContentView(this,
                R.layout.activity_gallery);

        ToolbarDelegate toolbarDelegate = new ToolbarDelegate(this, binding.toolbar);
        setTitle(null);
        toolbarDelegate.setupNavCrossIcon();

        // set Toolbar's padding because we use `android:windowTranslucentStatus` in this Activity
        // we only use translucent status if API >= 21
        ViewCompat.setOnApplyWindowInsetsListener(binding.toolbar, (v, insets) -> {
            int top = insets.getSystemWindowInsetTop();
            v.setPadding(0, top, 0, 0);
            v.getLayoutParams().height = v.getContext().getResources().getDimensionPixelSize(
                    R.dimen.abc_action_bar_default_height_material) + top;

            return insets.consumeSystemWindowInsets();
        });

        mImageUrl = getIntent().getStringExtra(ARG_IMAGE_URL);
        binding.setImageViewModel(new ImageViewModel(mImageUrl));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_gallery, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();

                return true;
            case R.id.menu_download:
                DownloadManager downloadManager = (DownloadManager)
                        getSystemService(Context.DOWNLOAD_SERVICE);
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mImageUrl));
                request.setNotificationVisibility(
                        DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                        mImageUrl.substring(mImageUrl.lastIndexOf("/") + 1));
                downloadManager.enqueue(request);

                return true;
            case R.id.menu_browser:
                IntentUtil.startViewIntentExcludeOurApp(this, Uri.parse(mImageUrl));

                return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -35,16 +35,41 @@
 ivity;%0A%0A
+import android.Manifest;%0A
 import a
@@ -155,24 +155,66 @@
 ent.Intent;%0A
+import android.content.pm.PackageManager;%0A
 import andro
@@ -322,24 +322,167 @@
 nvironment;%0A
+import android.support.annotation.NonNull;%0Aimport android.support.annotation.RequiresPermission;%0Aimport android.support.v4.app.ActivityCompat;%0A
 import andro
@@ -990,16 +990,87 @@
 vity %7B%0A%0A
+    private final static int REQUEST_CODE_WRITE_EXTERNAL_STORAGE = 0;%0A%0A
     priv
@@ -2922,32 +2922,1336 @@
                 
+if (ActivityCompat.checkSelfPermission(this,%0A                        Manifest.permission.WRITE_EXTERNAL_STORAGE) !=%0A                        PackageManager.PERMISSION_GRANTED) %7B%0A                    ActivityCompat.requestPermissions(this,%0A                            new String%5B%5D%7BManifest.permission.WRITE_EXTERNAL_STORAGE%7D,%0A                            REQUEST_CODE_WRITE_EXTERNAL_STORAGE);%0A%0A                    return true;%0A                %7D%0A%0A                downloadImage();%0A%0A                return true;%0A            case R.id.menu_browser:%0A                IntentUtil.startViewIntentExcludeOurApp(this, Uri.parse(mImageUrl));%0A%0A                return true;%0A        %7D%0A%0A        return super.onOptionsItemSelected(item);%0A    %7D%0A%0A    @Override%0A    public void onRequestPermissionsResult(int requestCode, @NonNull String%5B%5D permissions, @NonNull int%5B%5D grantResults) %7B%0A        if (requestCode == REQUEST_CODE_WRITE_EXTERNAL_STORAGE) %7B%0A            if (grantResults.length == 1 && grantResults%5B0%5D == PackageManager.PERMISSION_GRANTED) %7B%0A                downloadImage();%0A            %7D%0A        %7D else %7B%0A            super.onRequestPermissionsResult(requestCode, permissions, grantResults);%0A        %7D%0A    %7D%0A%0A    @RequiresPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)%0A    private void downloadImage() %7B%0A        
 DownloadManager 
@@ -4286,24 +4286,16 @@
 anager)%0A
-        
         
@@ -4346,24 +4346,16 @@
 RVICE);%0A
-        
         
@@ -4435,32 +4435,24 @@
 ImageUrl));%0A
-        
         requ
@@ -4498,24 +4498,16 @@
         
-        
 Download
@@ -4568,24 +4568,16 @@
         
-     
-   
 request.
@@ -4659,24 +4659,16 @@
         
-        
 mImageUr
@@ -4717,32 +4717,24 @@
 ));%0A        
- 
-       
 downloadMana
@@ -4758,250 +4758,8 @@
 st);
-%0A%0A                return true;%0A            case R.id.menu_browser:%0A                IntentUtil.startViewIntentExcludeOurApp(this, Uri.parse(mImageUrl));%0A%0A                return true;%0A        %7D%0A%0A        return super.onOptionsItemSelected(item);
 %0A   

Support runtime permissions if API >= 23