导航菜单

页面标题

页面副标题

Delta Chat v1.58.3 - MediaPreviewActivity.java 源代码

正在查看: Delta Chat v1.58.3 应用的 MediaPreviewActivity.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package org.thoughtcrime.securesms;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.b44t.messenger.DcChat;
import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcMediaGalleryElement;
import com.b44t.messenger.DcMsg;
import java.io.IOException;
import java.util.WeakHashMap;
import kotlinx.coroutines.internal.LockFreeTaskQueueCore;
import org.thoughtcrime.securesms.components.MediaView;
import org.thoughtcrime.securesms.components.viewpager.ExtendedOnPageChangedListener;
import org.thoughtcrime.securesms.connect.DcHelper;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.loaders.PagingMediaLoader;
import org.thoughtcrime.securesms.mms.GlideApp;
import org.thoughtcrime.securesms.mms.GlideRequests;
import org.thoughtcrime.securesms.mms.Slide;
import org.thoughtcrime.securesms.permissions.Permissions;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientModifiedListener;
import org.thoughtcrime.securesms.util.DateUtils;
import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.SaveAttachmentTask;
import org.thoughtcrime.securesms.util.StorageUtil;
import org.thoughtcrime.securesms.util.Util;

public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity implements RecipientModifiedListener, LoaderManager.LoaderCallbacks<DcMediaGalleryElement> {
    public static final String ACTIVITY_TITLE_EXTRA = "activity_title";
    public static final String ADDRESS_EXTRA = "address";
    public static final String DATE_EXTRA = "date";
    public static final String DC_MSG_ID = "dc_msg_id";
    public static final String EDIT_AVATAR_CHAT_ID = "avatar_for_chat_id";
    public static final String LEFT_IS_RECENT_EXTRA = "left_is_recent";
    public static final String OPENED_FROM_PROFILE = "opened_from_profile";
    public static final String OUTGOING_EXTRA = "outgoing";
    public static final String SIZE_EXTRA = "size";
    private static final String TAG = "MediaPreviewActivity";
    private Recipient conversationRecipient;
    private DcContext dcContext;
    private MediaItem initialMedia;
    private boolean leftIsRecent;
    private ViewPager mediaPager;
    private DcMsg messageRecord;
    private int restartItem = -1;
    private int editAvatarChatId = 0;

    interface MediaItemAdapter {
        MediaItem getMediaItemFor(int i);

        void pause(int i);
    }

    public void onLoaderReset(Loader<DcMediaGalleryElement> loader) {
    }

    public void onLoadFinished(Loader loader, Object obj) {
        onLoadFinished((Loader<DcMediaGalleryElement>) loader, (DcMediaGalleryElement) obj);
    }

    @Override
    protected void onPreCreate() {
        this.dynamicTheme = new DynamicTheme() {
            @Override
            public void onResume(Activity activity) {
            }

            @Override
            public void onCreate(Activity activity) {
                activity.setTheme(R.style.TextSecure_DarkTheme);
            }
        };
        super.onPreCreate();
    }

    @Override
    protected void onCreate(Bundle bundle, boolean z) {
        setFullscreenIfPossible();
        getWindow().setFlags(LockFreeTaskQueueCore.MIN_ADD_SPIN_CAPACITY, LockFreeTaskQueueCore.MIN_ADD_SPIN_CAPACITY);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.media_preview_activity);
        this.editAvatarChatId = getIntent().getIntExtra(EDIT_AVATAR_CHAT_ID, 0);
        String stringExtra = getIntent().getStringExtra(ACTIVITY_TITLE_EXTRA);
        if (stringExtra != null) {
            getSupportActionBar().setTitle(stringExtra);
        }
        initializeViews();
        initializeResources();
    }

    public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        Permissions.onRequestPermissionsResult((Activity) this, i, strArr, iArr);
    }

    private void setFullscreenIfPossible() {
        getWindow().getDecorView().setSystemUiVisibility(4);
    }

    @Override
    public void onModified(Recipient recipient) {
        Util.runOnMain(new Runnable() {
            @Override
            public final void run() {
                MediaPreviewActivity.this.initializeActionBar();
            }
        });
    }

    public void initializeActionBar() {
        String string;
        MediaItem currentMediaItem = getCurrentMediaItem();
        if (currentMediaItem != null) {
            if (currentMediaItem.date <= 0) {
                string = getString(R.string.draft);
            } else {
                string = DateUtils.getExtendedRelativeTimeSpanString(this, currentMediaItem.date);
            }
            if (currentMediaItem.outgoing) {
                getSupportActionBar().setTitle(getString(R.string.self));
            } else {
                getSupportActionBar().setTitle(this.dcContext.getContact(this.dcContext.getMsg(currentMediaItem.msgId).getFromId()).getDisplayName());
            }
            getSupportActionBar().setSubtitle(string);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initializeMedia();
    }

    public void onPause() {
        super.onPause();
        this.restartItem = cleanupMedia();
    }

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        initializeResources();
    }

    private void initializeViews() {
        ViewPager findViewById = findViewById(R.id.media_pager);
        this.mediaPager = findViewById;
        findViewById.setOffscreenPageLimit(1);
        this.mediaPager.addOnPageChangeListener(new ViewPagerListener());
    }

    private void initializeResources() {
        Address address = (Address) getIntent().getParcelableExtra(ADDRESS_EXTRA);
        Context applicationContext = getApplicationContext();
        this.dcContext = DcHelper.getContext(applicationContext);
        int intExtra = getIntent().getIntExtra(DC_MSG_ID, 0);
        if (intExtra == 0) {
            this.messageRecord = null;
            Recipient recipient = null;
            String str = null;
            int i = 0;
            this.initialMedia = new MediaItem(recipient, getIntent().getData(), str, getIntent().getType(), i, getIntent().getLongExtra(DATE_EXTRA, 0L), getIntent().getLongExtra(SIZE_EXTRA, 0L), false);
            if (address != null) {
                this.conversationRecipient = Recipient.from(applicationContext, address);
            } else {
                this.conversationRecipient = null;
            }
        } else {
            this.messageRecord = this.dcContext.getMsg(intExtra);
            this.initialMedia = new MediaItem(Recipient.fromChat(applicationContext, intExtra), Uri.fromFile(this.messageRecord.getFileAsFile()), this.messageRecord.getFilename(), this.messageRecord.getFilemime(), this.messageRecord.getId(), this.messageRecord.getDateReceived(), this.messageRecord.getFilebytes(), this.messageRecord.isOutgoing());
            this.conversationRecipient = Recipient.fromChat(applicationContext, intExtra);
        }
        this.leftIsRecent = getIntent().getBooleanExtra(LEFT_IS_RECENT_EXTRA, false);
        this.restartItem = -1;
    }

    private void initializeMedia() {
        Log.w(TAG, "Loading Part URI: " + this.initialMedia);
        if (this.messageRecord != null) {
            getSupportLoaderManager().restartLoader(0, (Bundle) null, this);
        } else {
            this.mediaPager.setAdapter(new SingleItemPagerAdapter(this, GlideApp.with((FragmentActivity) this), getWindow(), this.initialMedia.uri, this.initialMedia.name, this.initialMedia.type, this.initialMedia.size));
        }
    }

    private int cleanupMedia() {
        int currentItem = this.mediaPager.getCurrentItem();
        this.mediaPager.removeAllViews();
        this.mediaPager.setAdapter((PagerAdapter) null);
        return currentItem;
    }

    private void editAvatar() {
        Intent intent = new Intent((Context) this, (Class<?>) GroupCreateActivity.class);
        intent.putExtra(GroupCreateActivity.EDIT_GROUP_CHAT_ID, this.editAvatarChatId);
        startActivity(intent);
        finish();
    }

    private void showOverview() {
        if (getIntent().getBooleanExtra(OPENED_FROM_PROFILE, false)) {
            finish();
            return;
        }
        if (this.conversationRecipient.getAddress().isDcChat()) {
            Intent intent = new Intent((Context) this, (Class<?>) ProfileActivity.class);
            intent.putExtra("chat_id", this.conversationRecipient.getAddress().getDcChatId());
            intent.putExtra(ProfileActivity.FORCE_TAB_EXTRA, 20);
            startActivity(intent);
            finish();
            return;
        }
        if (this.conversationRecipient.getAddress().isDcContact()) {
            Intent intent2 = new Intent((Context) this, (Class<?>) ProfileActivity.class);
            intent2.putExtra("contact_id", this.conversationRecipient.getAddress().getDcContactId());
            intent2.putExtra(ProfileActivity.FORCE_TAB_EXTRA, 20);
            startActivity(intent2);
            finish();
        }
    }

    private void share() {
        MediaItem currentMediaItem = getCurrentMediaItem();
        if (currentMediaItem != null) {
            DcHelper.openForViewOrShare(this, currentMediaItem.msgId, "android.intent.action.SEND");
        }
    }

    private void saveToDisk() {
        final MediaItem currentMediaItem = getCurrentMediaItem();
        if (currentMediaItem != null) {
            SaveAttachmentTask.showWarningDialog(this, new DialogInterface.OnClickListener() {
                @Override
                public final void onClick(DialogInterface dialogInterface, int i) {
                    MediaPreviewActivity.this.m1256x69f0b664(currentMediaItem, dialogInterface, i);
                }
            });
        }
    }

    void m1256x69f0b664(final MediaItem mediaItem, DialogInterface dialogInterface, int i) {
        if (StorageUtil.canWriteToMediaStore(this)) {
            m1255x6a671c63(mediaItem);
        } else {
            Permissions.with((Activity) this).request("android.permission.WRITE_EXTERNAL_STORAGE").alwaysGrantOnSdk30().ifNecessary().withPermanentDenialDialog(getString(R.string.perm_explain_access_to_storage_denied)).onAllGranted(new Runnable() {
                @Override
                public final void run() {
                    MediaPreviewActivity.this.m1255x6a671c63(mediaItem);
                }
            }).execute();
        }
    }

    public void m1255x6a671c63(MediaItem mediaItem) {
        new SaveAttachmentTask(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new SaveAttachmentTask.Attachment[]{new SaveAttachmentTask.Attachment(mediaItem.uri, mediaItem.type, mediaItem.date > 0 ? mediaItem.date : System.currentTimeMillis(), mediaItem.name)});
    }

    private void showInChat() {
        MediaItem currentMediaItem = getCurrentMediaItem();
        if (currentMediaItem == null || currentMediaItem.msgId == 0) {
            Log.w(TAG, "mediaItem missing.");
            return;
        }
        DcMsg msg = this.dcContext.getMsg(currentMediaItem.msgId);
        if (msg.getId() == 0) {
            Log.w(TAG, "cannot get message object.");
            return;
        }
        Intent intent = new Intent((Context) this, (Class<?>) ConversationActivity.class);
        intent.putExtra("chat_id", msg.getChatId());
        intent.putExtra(ConversationActivity.STARTING_POSITION_EXTRA, DcMsg.getMessagePosition(msg, this.dcContext));
        startActivity(intent);
    }

    private void deleteMedia() {
        MediaItem currentMediaItem = getCurrentMediaItem();
        if (currentMediaItem == null || currentMediaItem.msgId == 0) {
            return;
        }
        DcMsg msg = this.dcContext.getMsg(currentMediaItem.msgId);
        DcChat chat = this.dcContext.getChat(msg.getChatId());
        String quantityString = getResources().getQuantityString(chat.isDeviceTalk() ? R.plurals.ask_delete_messages_simple : R.plurals.ask_delete_messages, 1, 1);
        int i = chat.isSelfTalk() ? R.string.delete : R.string.delete_for_me;
        final int[] iArr = {currentMediaItem.msgId};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(quantityString);
        builder.setCancelable(true);
        builder.setNeutralButton(android.R.string.cancel, (DialogInterface.OnClickListener) null);
        builder.setPositiveButton(i, new DialogInterface.OnClickListener() {
            @Override
            public final void onClick(DialogInterface dialogInterface, int i2) {
                MediaPreviewActivity.this.m1252x626cff0(iArr, dialogInterface, i2);
            }
        });
        if (chat.canSend() && !chat.isSelfTalk() && msg.isOutgoing()) {
            builder.setNegativeButton(R.string.delete_for_everyone, new DialogInterface.OnClickListener() {
                @Override
                public final void onClick(DialogInterface dialogInterface, int i2) {
                    MediaPreviewActivity.this.m1254x53a03f2(iArr, dialogInterface, i2);
                }
            });
            AlertDialog show = builder.show();
            Util.redButton(show, -2);
            Util.redPositiveButton(show);
            return;
        }
        Util.redPositiveButton(builder.show());
    }

    void m1251x69d35ef(int[] iArr) {
        this.dcContext.deleteMsgs(iArr);
    }

    void m1252x626cff0(final int[] iArr, DialogInterface dialogInterface, int i) {
        Util.runOnAnyBackgroundThread(new Runnable() {
            @Override
            public final void run() {
                MediaPreviewActivity.this.m1251x69d35ef(iArr);
            }
        });
        finish();
    }

    void m1253x5b069f1(int[] iArr) {
        this.dcContext.sendDeleteRequest(iArr);
    }

    void m1254x53a03f2(final int[] iArr, DialogInterface dialogInterface, int i) {
        Util.runOnAnyBackgroundThread(new Runnable() {
            @Override
            public final void run() {
                MediaPreviewActivity.this.m1253x5b069f1(iArr);
            }
        });
        finish();
    }

    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        menu.clear();
        getMenuInflater().inflate(R.menu.media_preview, menu);
        Util.redMenuItem(menu, R.id.delete);
        if (!isMediaInDb()) {
            menu.findItem(R.id.media_preview__overview).setVisible(false);
            menu.findItem(R.id.media_preview__share).setVisible(false);
            menu.findItem(R.id.delete).setVisible(false);
            menu.findItem(R.id.show_in_chat).setVisible(false);
        }
        if (this.editAvatarChatId != 0) {
            return true;
        }
        menu.findItem(R.id.media_preview__edit).setVisible(false);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        super.onOptionsItemSelected(menuItem);
        int itemId = menuItem.getItemId();
        if (itemId == R.id.media_preview__edit) {
            editAvatar();
            return true;
        }
        if (itemId == R.id.media_preview__overview) {
            showOverview();
            return true;
        }
        if (itemId == R.id.media_preview__share) {
            share();
            return true;
        }
        if (itemId == R.id.save) {
            saveToDisk();
            return true;
        }
        if (itemId == R.id.delete) {
            deleteMedia();
            return true;
        }
        if (itemId == R.id.show_in_chat) {
            showInChat();
            return true;
        }
        if (itemId != 16908332) {
            return false;
        }
        finish();
        return true;
    }

    private boolean isMediaInDb() {
        return this.conversationRecipient != null;
    }

    private MediaItem getCurrentMediaItem() {
        MediaItemAdapter adapter = this.mediaPager.getAdapter();
        if (adapter != null) {
            return adapter.getMediaItemFor(this.mediaPager.getCurrentItem());
        }
        return null;
    }

    public static boolean isTypeSupported(Slide slide) {
        return slide != null && (slide.hasVideo() || slide.hasImage());
    }

    public Loader<DcMediaGalleryElement> onCreateLoader(int i, Bundle bundle) {
        return new PagingMediaLoader(this, this.messageRecord, false);
    }

    public void onLoadFinished(Loader<DcMediaGalleryElement> loader, DcMediaGalleryElement dcMediaGalleryElement) {
        if (dcMediaGalleryElement != null) {
            DcMediaPagerAdapter dcMediaPagerAdapter = new DcMediaPagerAdapter(this, GlideApp.with((FragmentActivity) this), getWindow(), dcMediaGalleryElement, this.leftIsRecent);
            this.mediaPager.setAdapter(dcMediaPagerAdapter);
            dcMediaPagerAdapter.setActive(true);
            int i = this.restartItem;
            if (i < 0) {
                this.mediaPager.setCurrentItem(dcMediaGalleryElement.getPosition());
            } else {
                this.mediaPager.setCurrentItem(i);
            }
        }
    }

    private class ViewPagerListener extends ExtendedOnPageChangedListener {
        private ViewPagerListener() {
        }

        @Override
        public void onPageSelected(int i) {
            super.onPageSelected(i);
            MediaItemAdapter adapter = MediaPreviewActivity.this.mediaPager.getAdapter();
            if (adapter != null) {
                MediaItem mediaItemFor = adapter.getMediaItemFor(i);
                if (mediaItemFor.recipient != null) {
                    mediaItemFor.recipient.addListener(MediaPreviewActivity.this);
                }
                MediaPreviewActivity.this.initializeActionBar();
            }
        }

        @Override
        public void onPageUnselected(int i) {
            MediaItemAdapter adapter = MediaPreviewActivity.this.mediaPager.getAdapter();
            if (adapter != null) {
                try {
                    MediaItem mediaItemFor = adapter.getMediaItemFor(i);
                    if (mediaItemFor.recipient != null) {
                        mediaItemFor.recipient.removeListener(MediaPreviewActivity.this);
                    }
                } catch (IllegalArgumentException unused) {
                    Log.w(MediaPreviewActivity.TAG, "Ignoring invalid position index");
                }
                adapter.pause(i);
            }
        }
    }

    private static class SingleItemPagerAdapter extends PagerAdapter implements MediaItemAdapter {
        private final GlideRequests glideRequests;
        private final LayoutInflater inflater;
        private final String mediaType;
        private final String name;
        private final long size;
        private final Uri uri;
        private final Window window;

        public int getCount() {
            return 1;
        }

        public boolean isViewFromObject(View view, Object obj) {
            return view == obj;
        }

        @Override
        public void pause(int i) {
        }

        SingleItemPagerAdapter(Context context, GlideRequests glideRequests, Window window, Uri uri, String str, String str2, long j) {
            this.glideRequests = glideRequests;
            this.window = window;
            this.uri = uri;
            this.name = str;
            this.mediaType = str2;
            this.size = j;
            this.inflater = LayoutInflater.from(context);
        }

        public Object instantiateItem(ViewGroup viewGroup, int i) {
            View inflate = this.inflater.inflate(R.layout.media_view_page, viewGroup, false);
            try {
                ((MediaView) inflate.findViewById(R.id.media_view)).set(this.glideRequests, this.window, this.uri, this.name, this.mediaType, this.size, true);
            } catch (IOException e) {
                Log.w(MediaPreviewActivity.TAG, e);
            }
            viewGroup.addView(inflate);
            return inflate;
        }

        public void destroyItem(ViewGroup viewGroup, int i, Object obj) {
            FrameLayout frameLayout = (FrameLayout) obj;
            ((MediaView) frameLayout.findViewById(R.id.media_view)).cleanup();
            viewGroup.removeView(frameLayout);
        }

        @Override
        public MediaItem getMediaItemFor(int i) {
            return new MediaItem(null, this.uri, this.name, this.mediaType, 0, -1L, -1L, true);
        }
    }

    private static class DcMediaPagerAdapter extends PagerAdapter implements MediaItemAdapter {
        private boolean active;
        private int autoPlayPosition;
        private final Context context;
        private final DcMediaGalleryElement gallery;
        private final GlideRequests glideRequests;
        private final boolean leftIsRecent;
        private final WeakHashMap<Integer, MediaView> mediaViews = new WeakHashMap<>();
        private final Window window;

        public boolean isViewFromObject(View view, Object obj) {
            return view == obj;
        }

        DcMediaPagerAdapter(Context context, GlideRequests glideRequests, Window window, DcMediaGalleryElement dcMediaGalleryElement, boolean z) {
            this.context = context.getApplicationContext();
            this.glideRequests = glideRequests;
            this.window = window;
            this.gallery = dcMediaGalleryElement;
            this.leftIsRecent = z;
            this.autoPlayPosition = dcMediaGalleryElement.getPosition();
        }

        public void setActive(boolean z) {
            this.active = z;
            notifyDataSetChanged();
        }

        public int getCount() {
            if (this.active) {
                return this.gallery.getCount();
            }
            return 0;
        }

        public Object instantiateItem(ViewGroup viewGroup, int i) {
            View inflate = LayoutInflater.from(this.context).inflate(R.layout.media_view_page, viewGroup, false);
            MediaView mediaView = (MediaView) inflate.findViewById(R.id.media_view);
            boolean z = i == this.autoPlayPosition;
            int cursorPosition = getCursorPosition(i);
            this.autoPlayPosition = -1;
            this.gallery.moveToPosition(cursorPosition);
            DcMsg message = this.gallery.getMessage();
            try {
                mediaView.set(this.glideRequests, this.window, Uri.fromFile(message.getFileAsFile()), message.getFilename(), message.getFilemime(), message.getFilebytes(), z);
            } catch (IOException e) {
                Log.w(MediaPreviewActivity.TAG, e);
            }
            this.mediaViews.put(Integer.valueOf(i), mediaView);
            viewGroup.addView(inflate);
            return inflate;
        }

        public void destroyItem(ViewGroup viewGroup, int i, Object obj) {
            FrameLayout frameLayout = (FrameLayout) obj;
            ((MediaView) frameLayout.findViewById(R.id.media_view)).cleanup();
            this.mediaViews.remove(Integer.valueOf(i));
            viewGroup.removeView(frameLayout);
        }

        @Override
        public MediaItem getMediaItemFor(int i) {
            this.gallery.moveToPosition(getCursorPosition(i));
            DcMsg message = this.gallery.getMessage();
            if (message.getFile() == null) {
                throw new AssertionError();
            }
            return new MediaItem(Recipient.fromChat(this.context, message.getId()), Uri.fromFile(message.getFileAsFile()), message.getFilename(), message.getFilemime(), message.getId(), message.getDateReceived(), message.getFilebytes(), message.isOutgoing());
        }

        @Override
        public void pause(int i) {
            MediaView mediaView = this.mediaViews.get(Integer.valueOf(i));
            if (mediaView != null) {
                mediaView.pause();
            }
        }

        private int getCursorPosition(int i) {
            return this.leftIsRecent ? i : (this.gallery.getCount() - 1) - i;
        }
    }

    static class MediaItem {
        private final long date;
        private final int msgId;
        private final String name;
        private final boolean outgoing;
        private final Recipient recipient;
        private final long size;
        private final String type;
        private final Uri uri;

        private MediaItem(Recipient recipient, Uri uri, String str, String str2, int i, long j, long j2, boolean z) {
            this.recipient = recipient;
            this.uri = uri;
            this.name = str;
            this.type = str2;
            this.msgId = i;
            this.date = j;
            this.size = j2;
            this.outgoing = z;
        }
    }
}