|
| 1 | +package im.vector.activity; |
| 2 | + |
| 3 | +import android.net.Uri; |
| 4 | +import android.os.Build; |
| 5 | +import android.app.Activity; |
| 6 | +import android.support.v4.content.ContextCompat; |
| 7 | +import android.support.v7.widget.LinearLayoutManager; |
| 8 | +import android.support.v7.widget.RecyclerView; |
| 9 | +import android.text.TextUtils; |
| 10 | +import android.view.MotionEvent; |
| 11 | +import android.view.View; |
| 12 | +import android.view.Window; |
| 13 | +import android.view.WindowManager; |
| 14 | +import android.webkit.WebView; |
| 15 | +import android.widget.ImageView; |
| 16 | +import android.widget.TextView; |
| 17 | +import android.widget.VideoView; |
| 18 | + |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | +import org.matrix.androidsdk.data.RoomMediaMessage; |
| 21 | +import org.matrix.androidsdk.util.Log; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import butterknife.BindView; |
| 27 | +import butterknife.OnClick; |
| 28 | +import im.vector.R; |
| 29 | +import im.vector.adapters.MediaPreviewAdapter; |
| 30 | +import im.vector.listeners.ItemPositionChangedListener; |
| 31 | +import kotlin.Pair; |
| 32 | + |
| 33 | +/** |
| 34 | + * Previews media selected to be send. |
| 35 | + */ |
| 36 | +public class MediaPreviewerActivity extends MXCActionBarActivity implements ItemPositionChangedListener { |
| 37 | + |
| 38 | + /** |
| 39 | + * the picture uri if a picture is taken with the camera |
| 40 | + */ |
| 41 | + public static final String EXTRA_CAMERA_PICTURE_URI = "EXTRA_CAMERA_PICTURE_URI"; |
| 42 | + /** |
| 43 | + * the room title (string) |
| 44 | + */ |
| 45 | + public static final String EXTRA_ROOM_TITLE = "EXTRA_ROOM_TITLE"; |
| 46 | + |
| 47 | + private static final String LOG_TAG = MediaPreviewerActivity.class.getSimpleName(); |
| 48 | + |
| 49 | + @BindView(R.id.images_preview) |
| 50 | + RecyclerView mImagesPreview; |
| 51 | + |
| 52 | + @BindView(R.id.image_previewer) |
| 53 | + ImageView mImagePreview; |
| 54 | + |
| 55 | + @BindView(R.id.web_previewer) |
| 56 | + WebView mWebPreview; |
| 57 | + |
| 58 | + @BindView(R.id.video_previewer) |
| 59 | + VideoView mVideoPreview; |
| 60 | + |
| 61 | + @BindView(R.id.file_previewer) |
| 62 | + ImageView mFilePreview; |
| 63 | + |
| 64 | + @BindView(R.id.file_name) |
| 65 | + TextView mFileNameView; |
| 66 | + |
| 67 | + private RoomMediaMessage mCurrentRoomMediaMessage; |
| 68 | + |
| 69 | + List<RoomMediaMessage> mSharedDataItems; |
| 70 | + |
| 71 | + @NotNull |
| 72 | + @Override |
| 73 | + public Pair getOtherThemes() { |
| 74 | + return new Pair(R.style.AppTheme_NoActionBar_Dark, R.style.AppTheme_NoActionBar_Black); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public int getLayoutRes() { |
| 79 | + return R.layout.activity_media_previewer; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onItemPositionChangedListener(int position) { |
| 84 | + setPreview(mSharedDataItems.get(position)); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void initUiAndData() { |
| 89 | + |
| 90 | + if (CommonActivityUtils.shouldRestartApp(this)) { |
| 91 | + Log.d(LOG_TAG, "onCreate : restart the application"); |
| 92 | + CommonActivityUtils.restartApp(this); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + if (CommonActivityUtils.isGoingToSplash(this)) { |
| 97 | + Log.d(LOG_TAG, "onCreate : Going to splash screen"); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + configureToolbar(); |
| 102 | + |
| 103 | + String roomTitle = (String) getIntent().getExtras().get(EXTRA_ROOM_TITLE); |
| 104 | + if (!TextUtils.isEmpty(roomTitle)) { |
| 105 | + getSupportActionBar().setTitle(roomTitle); |
| 106 | + } |
| 107 | + |
| 108 | + setStatusBarColor(findViewById(R.id.status_bar_background), |
| 109 | + ContextCompat.getColor(this, R.color.transparent_dark)); |
| 110 | + |
| 111 | + // Resize web content to prevent scrollbars. |
| 112 | + mWebPreview.getSettings().setUseWideViewPort(true); |
| 113 | + mWebPreview.getSettings().setLoadWithOverviewMode(true); |
| 114 | + |
| 115 | + mSharedDataItems = new ArrayList<>(RoomMediaMessage.listRoomMediaMessages(getIntent(), RoomMediaMessage.class.getClassLoader())); |
| 116 | + |
| 117 | + if (mSharedDataItems.isEmpty()) { |
| 118 | + mSharedDataItems.add(new RoomMediaMessage(Uri.parse(getIntent().getStringExtra(EXTRA_CAMERA_PICTURE_URI)))); |
| 119 | + } |
| 120 | + |
| 121 | + if (!mSharedDataItems.isEmpty()) { |
| 122 | + LinearLayoutManager imagesPreviewLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); |
| 123 | + mImagesPreview.setLayoutManager(imagesPreviewLinearLayoutManager); |
| 124 | + |
| 125 | + MediaPreviewAdapter mediaPreviewAdapter = new MediaPreviewAdapter(mSharedDataItems, this); |
| 126 | + mImagesPreview.setAdapter(mediaPreviewAdapter); |
| 127 | + |
| 128 | + setPreview(mSharedDataItems.get(0)); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @OnClick(R.id.send_floating_action_button) |
| 133 | + public void onClick() { |
| 134 | + setResult(Activity.RESULT_OK, getIntent()); |
| 135 | + finish(); |
| 136 | + } |
| 137 | + |
| 138 | + private void setPreview(RoomMediaMessage roomMediaMessage) { |
| 139 | + |
| 140 | + // Prevent blinking when tapping on the same item multiple times. |
| 141 | + if (roomMediaMessage != mCurrentRoomMediaMessage) { |
| 142 | + mCurrentRoomMediaMessage = roomMediaMessage; |
| 143 | + |
| 144 | + mWebPreview.setVisibility(View.GONE); |
| 145 | + mImagePreview.setVisibility(View.GONE); |
| 146 | + mVideoPreview.setVisibility(View.GONE); |
| 147 | + mFilePreview.setVisibility(View.GONE); |
| 148 | + mFileNameView.setVisibility(View.GONE); |
| 149 | + |
| 150 | + String mimeType = roomMediaMessage.getMimeType(this); |
| 151 | + Uri uri = roomMediaMessage.getUri(); |
| 152 | + |
| 153 | + if (mimeType != null) { |
| 154 | + if (mimeType.startsWith("image")) { |
| 155 | + if (mimeType.endsWith("gif")) { |
| 156 | + mWebPreview.loadUrl(uri.toString()); |
| 157 | + mWebPreview.setVisibility(View.VISIBLE); |
| 158 | + } else { |
| 159 | + mImagePreview.setImageURI(uri); |
| 160 | + mImagePreview.setVisibility(View.VISIBLE); |
| 161 | + } |
| 162 | + } else if (mimeType.startsWith("video")) { |
| 163 | + mVideoPreview.setVideoURI(uri); |
| 164 | + mVideoPreview.seekTo(100); |
| 165 | + mVideoPreview.setVisibility(View.VISIBLE); |
| 166 | + |
| 167 | + // Pause/play video on click. |
| 168 | + mVideoPreview.setOnTouchListener(new View.OnTouchListener() { |
| 169 | + @Override |
| 170 | + public boolean onTouch(View view, MotionEvent motionEvent) { |
| 171 | + if (!mVideoPreview.isPlaying()) { |
| 172 | + mVideoPreview.start(); |
| 173 | + } else { |
| 174 | + mVideoPreview.pause(); |
| 175 | + } |
| 176 | + return false; |
| 177 | + } |
| 178 | + }); |
| 179 | + } else { |
| 180 | + // As other files can't be previewed, show a generic file image. |
| 181 | + mFilePreview.setVisibility(View.VISIBLE); |
| 182 | + } |
| 183 | + |
| 184 | + mFileNameView.setText(roomMediaMessage.getFileName(this)); |
| 185 | + mFileNameView.setVisibility(View.VISIBLE); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | + private void setStatusBarColor(View statusBar, int color) { |
| 192 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { |
| 193 | + Window window = getWindow(); |
| 194 | + window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, |
| 195 | + WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
| 196 | + |
| 197 | + int statusBarHeight = getStatusBarHeight(); |
| 198 | + |
| 199 | + statusBar.getLayoutParams().height = statusBarHeight; |
| 200 | + statusBar.setBackgroundColor(color); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + private int getStatusBarHeight() { |
| 205 | + int result = 0; |
| 206 | + int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); |
| 207 | + if (resourceId > 0) { |
| 208 | + result = getResources().getDimensionPixelSize(resourceId); |
| 209 | + } |
| 210 | + |
| 211 | + return result; |
| 212 | + } |
| 213 | +} |
0 commit comments