2017-06-25 07:07:41 +02:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.Manifest;
|
2017-07-14 07:26:58 +02:00
|
|
|
import android.animation.Animator;
|
|
|
|
import android.animation.AnimatorListenerAdapter;
|
2017-06-25 07:07:41 +02:00
|
|
|
import android.app.DownloadManager;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.PackageManager;
|
2017-07-14 07:26:58 +02:00
|
|
|
import android.graphics.Color;
|
2017-06-25 07:07:41 +02:00
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Environment;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.annotation.StringRes;
|
|
|
|
import android.support.design.widget.Snackbar;
|
|
|
|
import android.support.v4.app.ActivityCompat;
|
|
|
|
import android.support.v4.content.ContextCompat;
|
|
|
|
import android.support.v4.view.ViewPager;
|
|
|
|
import android.support.v7.app.ActionBar;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
2017-09-24 20:53:03 +02:00
|
|
|
import android.widget.Toast;
|
2017-06-25 07:07:41 +02:00
|
|
|
|
|
|
|
import com.keylesspalace.tusky.fragment.ViewMediaFragment;
|
|
|
|
import com.keylesspalace.tusky.pager.ImagePagerAdapter;
|
|
|
|
import com.keylesspalace.tusky.view.ImageViewPager;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
2017-07-14 07:26:58 +02:00
|
|
|
public class ViewMediaActivity extends BaseActivity implements ViewMediaFragment.PhotoActionsListener {
|
2017-06-25 07:07:41 +02:00
|
|
|
private static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
|
|
|
|
|
|
|
|
private ImageViewPager viewPager;
|
|
|
|
private View anyView;
|
|
|
|
private String[] imageUrls;
|
2017-07-14 07:26:58 +02:00
|
|
|
private Toolbar toolbar;
|
|
|
|
|
|
|
|
private boolean isToolbarVisible = true;
|
2017-06-25 07:07:41 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_view_media);
|
|
|
|
|
2017-07-14 07:26:58 +02:00
|
|
|
supportPostponeEnterTransition();
|
|
|
|
|
2017-06-25 07:07:41 +02:00
|
|
|
// Obtain the views.
|
2017-07-14 07:26:58 +02:00
|
|
|
toolbar = (Toolbar) findViewById(R.id.toolbar);
|
2017-06-25 07:07:41 +02:00
|
|
|
viewPager = (ImageViewPager) findViewById(R.id.view_pager);
|
|
|
|
anyView = toolbar;
|
|
|
|
|
|
|
|
// Gather the parameters.
|
|
|
|
Intent intent = getIntent();
|
|
|
|
imageUrls = intent.getStringArrayExtra("urls");
|
|
|
|
int initialPosition = intent.getIntExtra("urlIndex", 0);
|
|
|
|
|
|
|
|
// Setup the view pager.
|
|
|
|
final ImagePagerAdapter adapter = new ImagePagerAdapter(getSupportFragmentManager(),
|
2017-07-14 07:26:58 +02:00
|
|
|
imageUrls, initialPosition);
|
2017-06-25 07:07:41 +02:00
|
|
|
viewPager.setAdapter(adapter);
|
|
|
|
viewPager.setCurrentItem(initialPosition);
|
|
|
|
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onPageScrolled(int position, float positionOffset,
|
2017-07-14 07:26:58 +02:00
|
|
|
int positionOffsetPixels) {
|
|
|
|
}
|
2017-06-25 07:07:41 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageSelected(int position) {
|
|
|
|
CharSequence title = adapter.getPageTitle(position);
|
|
|
|
toolbar.setTitle(title);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-14 07:26:58 +02:00
|
|
|
public void onPageScrollStateChanged(int state) {
|
|
|
|
}
|
2017-06-25 07:07:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Setup the toolbar.
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
actionBar.setDisplayShowHomeEnabled(true);
|
|
|
|
actionBar.setTitle(adapter.getPageTitle(initialPosition));
|
|
|
|
}
|
|
|
|
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-07-14 07:26:58 +02:00
|
|
|
supportFinishAfterTransition();
|
2017-06-25 07:07:41 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
|
|
int id = item.getItemId();
|
|
|
|
switch (id) {
|
|
|
|
case R.id.action_download:
|
|
|
|
downloadImage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2017-07-14 07:26:58 +02:00
|
|
|
|
|
|
|
View decorView = getWindow().getDecorView();
|
|
|
|
int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;
|
|
|
|
decorView.setSystemUiVisibility(uiOptions);
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
getWindow().setStatusBarColor(Color.BLACK);
|
|
|
|
}
|
2017-06-25 07:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.view_media_toolbar, menu);
|
|
|
|
// Manually tint all action buttons, because the theme overlay doesn't handle them properly.
|
|
|
|
for (int i = 0; i < menu.size(); i++) {
|
|
|
|
Drawable drawable = menu.getItem(i).getIcon();
|
|
|
|
if (drawable != null) {
|
|
|
|
drawable.mutate();
|
|
|
|
int color = ContextCompat.getColor(this, R.color.text_color_primary_dark);
|
|
|
|
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-19 05:34:07 +02:00
|
|
|
@Override
|
|
|
|
public void onBringUp() {
|
|
|
|
supportStartPostponedEnterTransition();
|
|
|
|
}
|
|
|
|
|
2017-06-25 07:07:41 +02:00
|
|
|
@Override
|
|
|
|
public void onDismiss() {
|
2017-07-14 07:26:58 +02:00
|
|
|
supportFinishAfterTransition();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPhotoTap() {
|
|
|
|
isToolbarVisible = !isToolbarVisible;
|
|
|
|
final int visibility = isToolbarVisible ? View.VISIBLE : View.INVISIBLE;
|
|
|
|
int alpha = isToolbarVisible ? 1 : 0;
|
|
|
|
toolbar.animate().alpha(alpha)
|
|
|
|
.setListener(new AnimatorListenerAdapter() {
|
|
|
|
@Override
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
toolbar.setVisibility(visibility);
|
|
|
|
animation.removeListener(this);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.start();
|
2017-06-25 07:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
|
2017-07-14 07:26:58 +02:00
|
|
|
@NonNull int[] grantResults) {
|
2017-06-25 07:07:41 +02:00
|
|
|
switch (requestCode) {
|
|
|
|
case PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
|
|
|
|
if (grantResults.length > 0
|
|
|
|
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
downloadImage();
|
|
|
|
} else {
|
|
|
|
doErrorDialog(R.string.error_media_download_permission, R.string.action_retry,
|
|
|
|
new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
downloadImage();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void doErrorDialog(@StringRes int descriptionId, @StringRes int actionId,
|
2017-07-14 07:26:58 +02:00
|
|
|
View.OnClickListener listener) {
|
2017-06-25 07:07:41 +02:00
|
|
|
if (anyView != null) {
|
|
|
|
Snackbar bar = Snackbar.make(anyView, getString(descriptionId),
|
|
|
|
Snackbar.LENGTH_SHORT);
|
|
|
|
bar.setAction(actionId, listener);
|
|
|
|
bar.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void downloadImage() {
|
2017-07-18 21:32:43 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
|
2017-06-25 07:07:41 +02:00
|
|
|
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
2017-07-14 07:26:58 +02:00
|
|
|
!= PackageManager.PERMISSION_GRANTED) {
|
2017-06-25 07:07:41 +02:00
|
|
|
ActivityCompat.requestPermissions(this,
|
2017-07-14 07:26:58 +02:00
|
|
|
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
2017-06-25 07:07:41 +02:00
|
|
|
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
|
|
|
|
} else {
|
|
|
|
String url = imageUrls[viewPager.getCurrentItem()];
|
|
|
|
Uri uri = Uri.parse(url);
|
|
|
|
|
|
|
|
String filename = new File(url).getName();
|
|
|
|
|
2017-09-24 20:53:03 +02:00
|
|
|
String toastText = String.format(getResources().getString(R.string.download_image),
|
|
|
|
filename);
|
|
|
|
Toast.makeText(this.getApplicationContext(), toastText, Toast.LENGTH_SHORT).show();
|
|
|
|
|
2017-06-25 07:07:41 +02:00
|
|
|
DownloadManager downloadManager =
|
|
|
|
(DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
|
|
|
|
|
|
|
|
DownloadManager.Request request = new DownloadManager.Request(uri);
|
|
|
|
request.allowScanningByMediaScanner();
|
|
|
|
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,
|
|
|
|
getString(R.string.app_name) + "/" + filename);
|
|
|
|
|
|
|
|
downloadManager.enqueue(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|