rework MediaViewerActivity

This commit is contained in:
Christian Schneppe 2018-10-03 22:31:55 +02:00
parent 8e088ccdb7
commit 26cc9dcbea
40 changed files with 157 additions and 58 deletions

View file

@ -68,7 +68,7 @@ dependencies {
implementation "com.android.support:design:$supportLibVersion"
implementation "com.android.support:cardview-v7:$supportLibVersion"
implementation 'com.github.bumptech.glide:glide:3.8.0'
implementation 'com.github.chrisbanes:PhotoView:2.0.0'
implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.10.0'
implementation 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.3'
implementation 'pub.devrel:easypermissions:1.2.0'
implementation 'com.wefika:flowlayout:0.4.1'
@ -156,7 +156,7 @@ android {
}
lintOptions {
disable 'ExtraTranslation', 'MissingTranslation', 'InvalidPackage', 'MissingQuantity', 'AppCompatResource'
disable 'ExtraTranslation', 'MissingTranslation', 'InvalidPackage', 'MissingQuantity', 'AppCompatResource', 'RestrictedApi'
}
subprojects {

View file

@ -104,8 +104,8 @@
android:name=".ui.ConversationsActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:minHeight="300dp"
android:minWidth="300dp"
android:minHeight="300dp"
android:windowSoftInputMode="stateHidden"></activity>
<activity
android:name=".ui.ScanActivity"
@ -238,7 +238,7 @@
android:name=".ui.SearchActivity"
android:label="@string/search_messages" />
<activity
android:name=".ui.ShowFullscreenMessageActivity"
android:name=".ui.MediaViewerActivity"
android:configChanges="orientation|screenSize"
android:theme="@style/ConversationsTheme.FullScreen"></activity>
<activity

View file

@ -111,8 +111,7 @@ public class FileBackend {
}
}
public boolean deleteFile(Message message) {
File file = getFile(message);
public boolean deleteFile(File file) {
if (file.delete()) {
updateMediaScanner(file);
return true;
@ -121,6 +120,11 @@ public class FileBackend {
}
}
public boolean deleteFile(Message message) {
File file = getFile(message);
return deleteFile(file);
}
public DownloadableFile getFile(Message message) {
return getFile(message, true);
}
@ -219,8 +223,8 @@ public class FileBackend {
}
public static String getConversationsDirectory(final String type) {
if (type.equalsIgnoreCase("null") || type == null) {
return getAppMediaDirectory() + "Pix-Art Messenger" + "/";
if (type.equalsIgnoreCase("null")) {
return Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Pix-Art Messenger" + "/";
} else {
return getAppMediaDirectory() + "Pix-Art Messenger" + " " + type + "/";
}

View file

@ -625,7 +625,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
final boolean hideVoice = p.getBoolean("show_record_voice_btn", activity.getResources().getBoolean(R.bool.show_record_voice_btn));
PopupMenu popup = new PopupMenu(activity, v);
popup.inflate(R.menu.choose_attachment);
Menu menu = popup.getMenu();
final Menu menu = popup.getMenu();
ConversationMenuConfigurator.configureQuickShareAttachmentMenu(conversation, menu, hideVoice);
popup.setOnMenuItemClickListener(attachmentItem -> {
switch (attachmentItem.getItemId()) {

View file

@ -4,6 +4,8 @@ import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
@ -13,37 +15,40 @@ import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.view.menu.MenuPopupHelper;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.webkit.MimeTypeMap;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.github.chrisbanes.photoview.PhotoView;
import com.github.chrisbanes.photoview.PhotoViewAttacher;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.github.rtoshiro.view.video.FullscreenVideoLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import de.pixart.messenger.Config;
import de.pixart.messenger.R;
import de.pixart.messenger.persistance.FileBackend;
import de.pixart.messenger.utils.ExifHelper;
import de.pixart.messenger.utils.MimeUtils;
import static de.pixart.messenger.persistance.FileBackend.close;
public class ShowFullscreenMessageActivity extends XmppActivity {
public class MediaViewerActivity extends XmppActivity {
Integer oldOrientation;
PhotoView mImage;
SubsamplingScaleImageView mImage;
FullscreenVideoLayout mVideo;
ImageView mFullscreenbutton;
Uri mFileUri;
@ -52,6 +57,22 @@ public class ShowFullscreenMessageActivity extends XmppActivity {
int height = 0;
int width = 0;
int rotation = 0;
boolean isImage = false;
boolean isVideo = false;
public static String getMimeType(String path) {
try {
String type = null;
String extension = path.substring(path.lastIndexOf(".") + 1, path.length());
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void onCreate(Bundle savedInstanceState) {
@ -73,18 +94,59 @@ public class ShowFullscreenMessageActivity extends XmppActivity {
getWindow().setAttributes(layout);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_fullscreen_message);
setContentView(R.layout.activity_media_viewer);
mImage = findViewById(R.id.message_image_view);
mVideo = findViewById(R.id.message_video_view);
mFullscreenbutton = findViewById(R.id.vcv_img_fullscreen);
fab = findViewById(R.id.fab);
fab.setOnClickListener(v -> {
mVideo.reset();
shareWith(mFile);
PopupMenu popup = new PopupMenu(MediaViewerActivity.this, v);
popup.inflate(R.menu.media_viewer);
final Menu menu = popup.getMenu();
MenuItem delete = menu.findItem(R.id.action_delete);
MenuItem open = menu.findItem(R.id.action_open);
Log.d(Config.LOGTAG, "Path = " + mFile.toString());
if (mFile == null || !mFile.toString().startsWith("/") || mFile.toString().contains(FileBackend.getConversationsDirectory("null"))) {
delete.setVisible(true);
} else {
delete.setVisible(false);
}
if (isVideo) {
if (isDarkTheme()) {
open.setIcon(R.drawable.ic_video_white_24dp);
} else {
open.setIcon(R.drawable.ic_video_black_24dp);
}
} else if (isImage) {
if (isDarkTheme()) {
open.setIcon(R.drawable.ic_image_white_24dp);
} else {
open.setIcon(R.drawable.ic_image_black_24dp);
}
}
popup.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.action_share:
share();
break;
case R.id.action_open:
open();
break;
case R.id.action_delete:
deleteFile();
break;
default:
return false;
}
return true;
});
MenuPopupHelper menuHelper = new MenuPopupHelper(MediaViewerActivity.this, (MenuBuilder) menu, v);
menuHelper.setForceShowIcon(true);
menuHelper.show();
});
}
private void shareWith(File mFile) {
private void share() {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType(getMimeType(mFile.toString()));
share.putExtra(Intent.EXTRA_STREAM, FileBackend.getUriForFile(this, mFile));
@ -96,18 +158,35 @@ public class ShowFullscreenMessageActivity extends XmppActivity {
}
}
public static String getMimeType(String path) {
private void deleteFile() {
this.xmppConnectionService.getFileBackend().deleteFile(mFile);
finish();
}
private void open() {
Uri uri;
try {
String type = null;
String extension = path.substring(path.lastIndexOf(".") + 1, path.length());
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
} catch (Exception e) {
e.printStackTrace();
uri = FileBackend.getUriForFile(this, mFile);
} catch (SecurityException e) {
Log.d(Config.LOGTAG, "No permission to access " + mFile.getAbsolutePath(), e);
Toast.makeText(this, this.getString(R.string.no_permission_to_access_x, mFile.getAbsolutePath()), Toast.LENGTH_SHORT).show();
return;
}
String mime = MimeUtils.guessMimeTypeFromUri(this, uri);
Intent openIntent = new Intent(Intent.ACTION_VIEW);
openIntent.setDataAndType(uri, mime);
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
PackageManager manager = this.getPackageManager();
List<ResolveInfo> info = manager.queryIntentActivities(openIntent, 0);
if (info.size() == 0) {
openIntent.setDataAndType(uri, "*/*");
}
try {
this.startActivity(openIntent);
finish();
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
}
return null;
}
@Override
@ -125,34 +204,38 @@ public class ShowFullscreenMessageActivity extends XmppActivity {
mFile = new File(mFileUri.getPath());
if (mFileUri != null && mFile.exists() && mFile.length() > 0) {
try {
DisplayImage(mFile);
isImage = true;
DisplayImage(mFile, mFileUri);
} catch (Exception e) {
isImage = false;
Log.d(Config.LOGTAG, "Illegal exeption :" + e);
Toast.makeText(ShowFullscreenMessageActivity.this, getString(R.string.error_file_corrupt), Toast.LENGTH_SHORT).show();
Toast.makeText(MediaViewerActivity.this, getString(R.string.error_file_corrupt), Toast.LENGTH_SHORT).show();
finish();
}
} else {
Toast.makeText(ShowFullscreenMessageActivity.this, getString(R.string.file_deleted), Toast.LENGTH_SHORT).show();
Toast.makeText(MediaViewerActivity.this, getString(R.string.file_deleted), Toast.LENGTH_SHORT).show();
}
} else if (intent.hasExtra("video")) {
mFileUri = intent.getParcelableExtra("video");
mFile = new File(mFileUri.getPath());
if (mFileUri != null && mFile.exists() && mFile.length() > 0) {
try {
isVideo = true;
DisplayVideo(mFileUri);
} catch (Exception e) {
isVideo = false;
Log.d(Config.LOGTAG, "Illegal exeption :" + e);
Toast.makeText(ShowFullscreenMessageActivity.this, getString(R.string.error_file_corrupt), Toast.LENGTH_SHORT).show();
Toast.makeText(MediaViewerActivity.this, getString(R.string.error_file_corrupt), Toast.LENGTH_SHORT).show();
finish();
}
} else {
Toast.makeText(ShowFullscreenMessageActivity.this, getString(R.string.file_deleted), Toast.LENGTH_SHORT).show();
Toast.makeText(MediaViewerActivity.this, getString(R.string.file_deleted), Toast.LENGTH_SHORT).show();
}
}
}
}
private void DisplayImage(final File file) {
private void DisplayImage(final File file, final Uri FileUri) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(new File(file.getPath()).getAbsolutePath(), options);
@ -163,19 +246,9 @@ public class ShowFullscreenMessageActivity extends XmppActivity {
if (useAutoRotateScreen()) {
rotateScreen(width, height, rotation);
}
final PhotoViewAttacher mAttacher = new PhotoViewAttacher(mImage);
mImage.setVisibility(View.VISIBLE);
try {
Glide.with(this)
.load(file)
.dontAnimate()
.into(new GlideDrawableImageViewTarget(mImage) {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
super.onResourceReady(resource, animation);
mAttacher.update();
}
});
mImage.setImage(ImageSource.uri(FileUri));
} catch (Exception e) {
Toast.makeText(this, getString(R.string.error_file_corrupt), Toast.LENGTH_LONG).show();
e.printStackTrace();

View file

@ -15,7 +15,7 @@ import java.util.List;
import de.pixart.messenger.Config;
import de.pixart.messenger.R;
import de.pixart.messenger.persistance.FileBackend;
import de.pixart.messenger.ui.ShowFullscreenMessageActivity;
import de.pixart.messenger.ui.MediaViewerActivity;
public class ViewUtil {
@ -36,7 +36,7 @@ public class ViewUtil {
}
// use internal viewer for images and videos
if (mime.startsWith("image/")) {
Intent intent = new Intent(context, ShowFullscreenMessageActivity.class);
Intent intent = new Intent(context, MediaViewerActivity.class);
intent.putExtra("image", Uri.fromFile(file));
try {
context.startActivity(intent);
@ -45,7 +45,7 @@ public class ViewUtil {
//ignored
}
} else if (mime.startsWith("video/")) {
Intent intent = new Intent(context, ShowFullscreenMessageActivity.class);
Intent intent = new Intent(context, MediaViewerActivity.class);
intent.putExtra("video", Uri.fromFile(file));
try {
context.startActivity(intent);

Binary file not shown.

After

(image error) Size: 492 B

Binary file not shown.

After

(image error) Size: 417 B

Binary file not shown.

After

(image error) Size: 753 B

Binary file not shown.

After

(image error) Size: 237 B

Binary file not shown.

After

(image error) Size: 239 B

Binary file not shown.

After

(image error) Size: 410 B

Binary file not shown.

After

(image error) Size: 366 B

Binary file not shown.

After

(image error) Size: 573 B

Binary file not shown.

After

(image error) Size: 146 B

Binary file not shown.

After

(image error) Size: 150 B

Binary file not shown.

After

(image error) Size: 479 B

Binary file not shown.

After

(image error) Size: 386 B

Binary file not shown.

After

(image error) Size: 852 B

Binary file not shown.

After

(image error) Size: 212 B

Binary file not shown.

After

(image error) Size: 206 B

Binary file not shown.

After

(image error) Size: 575 B

Binary file not shown.

After

(image error) Size: 420 B

Binary file not shown.

After

(image error) Size: 1.2 KiB

Binary file not shown.

After

(image error) Size: 325 B

Binary file not shown.

After

(image error) Size: 332 B

Binary file not shown.

After

(image error) Size: 668 B

Binary file not shown.

After

(image error) Size: 450 B

Binary file not shown.

After

(image error) Size: 1.5 KiB

Binary file not shown.

After

(image error) Size: 425 B

Binary file not shown.

After

(image error) Size: 419 B

View file

@ -13,7 +13,7 @@
android:layout_alignParentRight="true"
android:layout_alignParentTop="true">
<com.github.chrisbanes.photoview.PhotoView
<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
android:id="@id/message_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -32,6 +32,6 @@
android:layout_height="wrap_content"
android:layout_gravity="right|top"
android:layout_margin="16dp"
android:src="?attr/icon_share" />
android:src="@drawable/ic_menu_white_24dp" />
</FrameLayout>
</RelativeLayout>

View file

@ -11,7 +11,7 @@
<item
android:id="@+id/action_share"
android:icon="?attr/icon_share"
android:icon="@drawable/ic_share_white_24dp"
android:orderInCategory="15"
app:showAsAction="always"
android:title="@string/share_uri_with">

View file

@ -9,7 +9,7 @@
<item android:id="@+id/action_share"
android:title="@string/share_uri_with"
android:icon="?attr/icon_share"
android:icon="@drawable/ic_share_white_24dp"
app:showAsAction="always">
<menu>
<item

View file

@ -0,0 +1,17 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_share"
android:icon="?attr/icon_share"
android:orderInCategory="10"
android:title="@string/share" />
<item
android:id="@+id/action_open"
android:orderInCategory="20"
android:title="@string/action_open" />
<item
android:id="@+id/action_delete"
android:icon="?attr/icon_delete"
android:orderInCategory="30"
android:title="@string/action_delete" />
</menu>

View file

@ -4,7 +4,7 @@
<item
android:id="@+id/action_share"
android:icon="?attr/icon_share"
android:icon="@drawable/ic_share_white_24dp"
android:orderInCategory="15"
android:title="@string/share_uri_with"
app:showAsAction="always">

View file

@ -75,6 +75,7 @@
<attr name="icon_secure" format="reference" />
<attr name="icon_settings" format="reference" />
<attr name="icon_share" format="reference" />
<attr name="icon_delete" format="reference" />
<attr name="icon_import_export" format="reference" />
<attr name="icon_scan_qr_code" format="reference" />
<attr name="icon_enable_undecided_device" format="reference" />

View file

@ -41,7 +41,7 @@
\n\nhttp://hc.apache.org/httpcomponents-client\n(Apache License, Version 2.0)
\n\nhttp://hc.apache.org/httpcomponents-core\n(Apache License, Version 2.0)
\n\nhttps://github.com/bumptech/glide\n(BSD, The MIT License (MIT) and Apache License, Version 2.0)
\n\nhttps://github.com/chrisbanes/PhotoView\n(Apache License, Version 2.0)
\n\nhttps://github.com/davemorrissey/subsampling-scale-image-view\n(Apache License, Version 2.0)
\n\nhttps://github.com/rtoshiro/FullscreenVideoView\n(Apache License, Version 2.0)
\n\nhttps://github.com/mangstadt/ez-vcard\n(FreeBSD)
\n\nhttps://github.com/googlesamples/easypermissions\n(Apache License, Version 2.0)

View file

@ -827,4 +827,6 @@
<string name="view_media">View media</string>
<string name="media_browser">Media browser</string>
<string name="account_status_stream_opening_error">Stream opening error</string>
<string name="action_open">Open</string>
<string name="action_delete">Delete</string>
</resources>

View file

@ -99,7 +99,8 @@
<item name="icon_settings" type="reference">@drawable/ic_settings_black_24dp</item>
<item name="icon_import_export" type="reference">@drawable/ic_import_export_white_24dp
</item>
<item name="icon_share" type="reference">@drawable/ic_share_white_24dp</item>
<item name="icon_delete" type="reference">@drawable/ic_delete_black_24dp</item>
<item name="icon_share" type="reference">@drawable/ic_share_black_24dp</item>
<item name="icon_scan_qr_code" type="reference">@drawable/ic_barcode_scan_white_24dp</item>
<item name="icon_scroll_down" type="reference">@drawable/ic_scroll_to_end_black</item>
@ -271,6 +272,7 @@
<item name="icon_settings" type="reference">@drawable/ic_settings_white_24dp</item>
<item name="icon_import_export" type="reference">@drawable/ic_import_export_white_24dp
</item>
<item name="icon_delete" type="reference">@drawable/ic_delete_white_24dp</item>
<item name="icon_share" type="reference">@drawable/ic_share_white_24dp</item>
<item name="icon_scan_qr_code" type="reference">@drawable/ic_barcode_scan_white_24dp</item>
<item name="icon_scroll_down" type="reference">@drawable/ic_scroll_to_end_white</item>