Java Code Examples for com.zhihu.matisse.internal.entity.Item

The following examples show how to use com.zhihu.matisse.internal.entity.Item. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Matisse   Source File: SelectedItemCollection.java    License: Apache License 2.0 6 votes vote down vote up
public boolean add(Item item) {
    if (typeConflict(item)) {
        throw new IllegalArgumentException("Can't select images and videos at the same time.");
    }
    boolean added = mItems.add(item);
    if (added) {
        if (mCollectionType == COLLECTION_UNDEFINED) {
            if (item.isImage()) {
                mCollectionType = COLLECTION_IMAGE;
            } else if (item.isVideo()) {
                mCollectionType = COLLECTION_VIDEO;
            }
        } else if (mCollectionType == COLLECTION_IMAGE) {
            if (item.isVideo()) {
                mCollectionType = COLLECTION_MIXED;
            }
        } else if (mCollectionType == COLLECTION_VIDEO) {
            if (item.isImage()) {
                mCollectionType = COLLECTION_MIXED;
            }
        }
    }
    return added;
}
 
Example 2
Source Project: Matisse   Source File: SelectedItemCollection.java    License: Apache License 2.0 6 votes vote down vote up
public IncapableCause isAcceptable(Item item) {
    if (maxSelectableReached()) {
        int maxSelectable = currentMaxSelectable();
        String cause;

        try {
            cause = mContext.getString(
                    R.string.error_over_count,
                    maxSelectable
            );
        } catch (Resources.NotFoundException e) {
            cause = mContext.getString(
                    R.string.error_over_count,
                    maxSelectable
            );
        }

        return new IncapableCause(cause);
    } else if (typeConflict(item)) {
        return new IncapableCause(mContext.getString(R.string.error_type_conflict));
    }

    return PhotoMetadataUtils.isAcceptable(mContext, item);
}
 
Example 3
Source Project: Matisse   Source File: AlbumMediaAdapter.java    License: Apache License 2.0 6 votes vote down vote up
public void refreshSelection() {
    GridLayoutManager layoutManager = (GridLayoutManager) mRecyclerView.getLayoutManager();
    int first = layoutManager.findFirstVisibleItemPosition();
    int last = layoutManager.findLastVisibleItemPosition();
    if (first == -1 || last == -1) {
        return;
    }
    Cursor cursor = getCursor();
    for (int i = first; i <= last; i++) {
        RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForAdapterPosition(first);
        if (holder instanceof MediaViewHolder) {
            if (cursor.moveToPosition(i)) {
                setCheckStatus(Item.valueOf(cursor), ((MediaViewHolder) holder).mMediaGrid);
            }
        }
    }
}
 
Example 4
Source Project: Matisse   Source File: AlbumPreviewActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
    public void onAlbumMediaLoad(Cursor cursor) {
        List<Item> items = new ArrayList<>();
        while (cursor.moveToNext()) {
            items.add(Item.valueOf(cursor));
        }
//        cursor.close();

        if (items.isEmpty()) {
            return;
        }

        PreviewPagerAdapter adapter = (PreviewPagerAdapter) mPager.getAdapter();
        adapter.addAll(items);
        adapter.notifyDataSetChanged();
        if (!mIsAlreadySetPosition) {
            //onAlbumMediaLoad is called many times..
            mIsAlreadySetPosition = true;
            Item selected = getIntent().getParcelableExtra(EXTRA_ITEM);
            int selectedIndex = items.indexOf(selected);
            mPager.setCurrentItem(selectedIndex, false);
            mPreviousPos = selectedIndex;
        }
    }
 
Example 5
Source Project: Matisse   Source File: BasePreviewActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(View v) {
    if (v.getId() == R.id.button_back) {
        onBackPressed();
    } else if (v.getId() == R.id.button_apply) {
        Item item = mAdapter.getMediaItem(mPager.getCurrentItem());
        if (mSpec.isCrop && mSpec.maxSelectable <= 1 && !item.mimeType.toLowerCase().contains("video")) {
            Intent intentCrop = new Intent(this, ImageCropActivity.class);
            intentCrop.putExtra(MatisseActivity.EXTRA_RESULT_SELECTION_PATH,
                    PathUtils.getPath(this, item.getContentUri()));
            startActivityForResult(intentCrop, REQUEST_CODE_CROP);
        } else {
            sendBackResult(true);
            finish();
        }
    }
}
 
Example 6
Source Project: Matisse   Source File: SelectedPreviewActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bundle = getIntent().getBundleExtra(EXTRA_DEFAULT_BUNDLE);
    List<Item> selected = bundle.getParcelableArrayList(SelectedItemCollection.STATE_SELECTION);
    mAdapter.addAll(selected);
    mAdapter.notifyDataSetChanged();
    if (mSpec.countable) {
        mCheckView.setCheckedNum(1);
    } else {
        mCheckView.setChecked(true);
    }
    mPreviousPos = 0;
    updateSize(selected.get(0));
}
 
Example 7
Source Project: Matisse   Source File: AlbumMediaAdapter.java    License: Apache License 2.0 6 votes vote down vote up
public void refreshSelection() {
    GridLayoutManager layoutManager = (GridLayoutManager) mRecyclerView.getLayoutManager();
    int first = layoutManager.findFirstVisibleItemPosition();
    int last = layoutManager.findLastVisibleItemPosition();
    if (first == -1 || last == -1) {
        return;
    }
    Cursor cursor = getCursor();
    for (int i = first; i <= last; i++) {
        RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForAdapterPosition(first);
        if (holder instanceof MediaViewHolder) {
            if (cursor.moveToPosition(i)) {
                setCheckStatus(Item.valueOf(cursor), ((MediaViewHolder) holder).mMediaGrid);
            }
        }
    }
}
 
Example 8
Source Project: Matisse   Source File: AlbumPreviewActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mCollection.onCreate(this, this);
    Album album = getIntent().getParcelableExtra(EXTRA_ALBUM);
    mCollection.load(album);

    Item item = getIntent().getParcelableExtra(EXTRA_ITEM);
    if (mSpec.countable) {
        mCheckView.setCheckedNum(mSelectedCollection.checkedNumOf(item));
    } else {
        mCheckView.setChecked(mSelectedCollection.isSelected(item));
    }
    updateSize(item);
}
 
Example 9
Source Project: Matisse   Source File: AlbumPreviewActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onAlbumMediaLoad(Cursor cursor) {
    List<Item> items = new ArrayList<>();
    while (cursor.moveToNext()) {
        items.add(Item.valueOf(cursor));
    }
    PreviewPagerAdapter adapter = (PreviewPagerAdapter) mPager.getAdapter();
    adapter.addAll(items);
    adapter.notifyDataSetChanged();
    if (!mIsAlreadySetPosition) {
        //onAlbumMediaLoad is called many times..
        mIsAlreadySetPosition = true;
        Item selected = getIntent().getParcelableExtra(EXTRA_ITEM);
        int selectedIndex = items.indexOf(selected);
        mPager.setCurrentItem(selectedIndex, false);
        mPreviousPos = selectedIndex;
    }
}
 
Example 10
Source Project: Matisse   Source File: AlbumPreviewActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (!SelectionSpec.getInstance().hasInited) {
        setResult(RESULT_CANCELED);
        finish();
        return;
    }
    mCollection.onCreate(this, this);
    Album album = getIntent().getParcelableExtra(EXTRA_ALBUM);
    mCollection.load(album);

    Item item = getIntent().getParcelableExtra(EXTRA_ITEM);
    if (mSpec.countable) {
        mCheckView.setCheckedNum(mSelectedCollection.checkedNumOf(item));
    } else {
        mCheckView.setChecked(mSelectedCollection.isSelected(item));
    }
    updateSize(item);
}
 
Example 11
Source Project: Matisse   Source File: SelectedItemCollection.java    License: Apache License 2.0 6 votes vote down vote up
public boolean add(Item item) {
    if (typeConflict(item)) {
        throw new IllegalArgumentException("Can't select images and videos at the same time.");
    }
    boolean added = mItems.add(item);
    if (added) {
        if (mCollectionType == COLLECTION_UNDEFINED) {
            if (item.isImage()) {
                mCollectionType = COLLECTION_IMAGE;
            } else if (item.isVideo()) {
                mCollectionType = COLLECTION_VIDEO;
            }
        } else if (mCollectionType == COLLECTION_IMAGE) {
            if (item.isVideo()) {
                mCollectionType = COLLECTION_MIXED;
            }
        } else if (mCollectionType == COLLECTION_VIDEO) {
            if (item.isImage()) {
                mCollectionType = COLLECTION_MIXED;
            }
        }
    }
    return added;
}
 
Example 12
Source Project: Matisse   Source File: SelectedPreviewActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (!SelectionSpec.getInstance().hasInited) {
        setResult(RESULT_CANCELED);
        finish();
        return;
    }

    Bundle bundle = getIntent().getBundleExtra(EXTRA_DEFAULT_BUNDLE);
    List<Item> selected = bundle.getParcelableArrayList(SelectedItemCollection.STATE_SELECTION);
    mAdapter.addAll(selected);
    mAdapter.notifyDataSetChanged();
    if (mSpec.countable) {
        mCheckView.setCheckedNum(1);
    } else {
        mCheckView.setChecked(true);
    }
    mPreviousPos = 0;
    updateSize(selected.get(0));
}
 
Example 13
Source Project: Matisse   Source File: AlbumMediaAdapter.java    License: Apache License 2.0 6 votes vote down vote up
private void updateSelectedItem(Item item, RecyclerView.ViewHolder holder) {
    if (mSelectionSpec.countable) {
        int checkedNum = mSelectedCollection.checkedNumOf(item);
        if (checkedNum == CheckView.UNCHECKED) {
            if (assertAddSelection(holder.itemView.getContext(), item)) {
                mSelectedCollection.add(item);
                notifyCheckStateChanged();
            }
        } else {
            mSelectedCollection.remove(item);
            notifyCheckStateChanged();
        }
    } else {
        if (mSelectedCollection.isSelected(item)) {
            mSelectedCollection.remove(item);
            notifyCheckStateChanged();
        } else {
            if (assertAddSelection(holder.itemView.getContext(), item)) {
                mSelectedCollection.add(item);
                notifyCheckStateChanged();
            }
        }
    }
}
 
Example 14
Source Project: Matisse   Source File: GifSizeFilter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IncapableCause filter(Context context, Item item) {
    if (!needFiltering(context, item))
        return null;

    Point size = PhotoMetadataUtils.getBitmapBound(context.getContentResolver(), item.getContentUri());
    if (size.x < mMinWidth || size.y < mMinHeight || item.size > mMaxSize) {
        return new IncapableCause(IncapableCause.DIALOG, context.getString(R.string.error_gif, mMinWidth,
                String.valueOf(PhotoMetadataUtils.getSizeInMB(mMaxSize))));
    }
    return null;
}
 
Example 15
Source Project: Matisse   Source File: MatisseActivity.java    License: Apache License 2.0 5 votes vote down vote up
private int countOverMaxSize() {
    int count = 0;
    int selectedCount = mSelectedCollection.count();
    for (int i = 0; i < selectedCount; i++) {
        Item item = mSelectedCollection.asList().get(i);

        if (item.isImage()) {
            float size = PhotoMetadataUtils.getSizeInMB(item.size);
            if (size > mSpec.originalMaxSize) {
                count++;
            }
        }
    }
    return count;
}
 
Example 16
Source Project: Matisse   Source File: MatisseActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onMediaClick(Album album, Item item, int adapterPosition) {
    Intent intent = new Intent(this, AlbumPreviewActivity.class);
    intent.putExtra(AlbumPreviewActivity.EXTRA_ALBUM, album);
    intent.putExtra(AlbumPreviewActivity.EXTRA_ITEM, item);
    intent.putExtra(BasePreviewActivity.EXTRA_DEFAULT_BUNDLE, mSelectedCollection.getDataWithBundle());
    startActivityForResult(intent, REQUEST_CODE_PREVIEW);
}
 
Example 17
Source Project: Matisse   Source File: AlbumMediaLoader.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Cursor loadInBackground() {
    Cursor result = super.loadInBackground();
    if (!mEnableCapture || !MediaStoreCompat.hasCameraFeature(getContext())) {
        return result;
    }
    MatrixCursor dummy = new MatrixCursor(PROJECTION);
    dummy.addRow(new Object[]{Item.ITEM_ID_CAPTURE, Item.ITEM_DISPLAY_NAME_CAPTURE, "", 0, 0});
    return new MergeCursor(new Cursor[]{dummy, result});
}
 
Example 18
Source Project: Matisse   Source File: PhotoMetadataUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static IncapableCause isAcceptable(Context context, Item item) {
    if (!isSelectableType(context, item)) {
        return new IncapableCause(context.getString(R.string.error_file_type));
    }

    if (SelectionSpec.getInstance().filters != null) {
        for (Filter filter : SelectionSpec.getInstance().filters) {
            IncapableCause incapableCause = filter.filter(context, item);
            if (incapableCause != null) {
                return incapableCause;
            }
        }
    }
    return null;
}
 
Example 19
Source Project: Matisse   Source File: PhotoMetadataUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean isSelectableType(Context context, Item item) {
    if (context == null) {
        return false;
    }

    ContentResolver resolver = context.getContentResolver();
    for (MimeType type : SelectionSpec.getInstance().mimeTypeSet) {
        if (type.checkType(resolver, item.getContentUri())) {
            return true;
        }
    }
    return false;
}
 
Example 20
Source Project: Matisse   Source File: SelectedItemCollection.java    License: Apache License 2.0 5 votes vote down vote up
public void onCreate(Bundle bundle) {
    if (bundle == null) {
        mItems = new LinkedHashSet<>();
    } else {
        List<Item> saved = bundle.getParcelableArrayList(STATE_SELECTION);
        mItems = new LinkedHashSet<>(saved);
        mCollectionType = bundle.getInt(STATE_COLLECTION_TYPE, COLLECTION_UNDEFINED);
    }
}
 
Example 21
Source Project: Matisse   Source File: SelectedItemCollection.java    License: Apache License 2.0 5 votes vote down vote up
public boolean remove(Item item) {
    boolean removed = mItems.remove(item);
    if (removed) {
        if (mItems.size() == 0) {
            mCollectionType = COLLECTION_UNDEFINED;
        } else {
            if (mCollectionType == COLLECTION_MIXED) {
                refineCollectionType();
            }
        }
    }
    return removed;
}
 
Example 22
Source Project: Matisse   Source File: SelectedItemCollection.java    License: Apache License 2.0 5 votes vote down vote up
public void overwrite(ArrayList<Item> items, int collectionType) {
    if (items.size() == 0) {
        mCollectionType = COLLECTION_UNDEFINED;
    } else {
        mCollectionType = collectionType;
    }
    mItems.clear();
    mItems.addAll(items);
}
 
Example 23
Source Project: Matisse   Source File: SelectedItemCollection.java    License: Apache License 2.0 5 votes vote down vote up
public List<Uri> asListOfUri() {
    List<Uri> uris = new ArrayList<>();
    for (Item item : mItems) {
        uris.add(item.getContentUri());
    }
    return uris;
}
 
Example 24
Source Project: Matisse   Source File: SelectedItemCollection.java    License: Apache License 2.0 5 votes vote down vote up
public List<String> asListOfString() {
    List<String> paths = new ArrayList<>();
    for (Item item : mItems) {
        paths.add(PathUtils.getPath(mContext, item.getContentUri()));
    }
    return paths;
}
 
Example 25
Source Project: Matisse   Source File: BasePreviewActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onPageSelected(int position) {
    PreviewPagerAdapter adapter = (PreviewPagerAdapter) mPager.getAdapter();
    if (mPreviousPos != -1 && mPreviousPos != position) {
        ((PreviewItemFragment) adapter.instantiateItem(mPager, mPreviousPos)).resetView();

        Item item = adapter.getMediaItem(position);
        if (mSpec.countable) {
            int checkedNum = mSelectedCollection.checkedNumOf(item);
            mCheckView.setCheckedNum(checkedNum);
            if (checkedNum > 0) {
                mCheckView.setEnabled(true);
            } else {
                mCheckView.setEnabled(!mSelectedCollection.maxSelectableReached());
            }
        } else {
            boolean checked = mSelectedCollection.isSelected(item);
            mCheckView.setChecked(checked);
            if (checked) {
                mCheckView.setEnabled(true);
            } else {
                mCheckView.setEnabled(!mSelectedCollection.maxSelectableReached());
            }
        }
        updateSize(item);
    }
    mPreviousPos = position;
}
 
Example 26
Source Project: Matisse   Source File: BasePreviewActivity.java    License: Apache License 2.0 5 votes vote down vote up
private int countOverMaxSize() {
    int count = 0;
    int selectedCount = mSelectedCollection.count();
    for (int i = 0; i < selectedCount; i++) {
        Item item = mSelectedCollection.asList().get(i);
        if(item.isImage()){
            float size = PhotoMetadataUtils.getSizeInMB(item.size);
            if (size > mSpec.originalMaxSize) {
                count++;
            }
        }
    }
    return count;
}
 
Example 27
Source Project: Matisse   Source File: BasePreviewActivity.java    License: Apache License 2.0 5 votes vote down vote up
protected void updateSize(Item item) {
    if (item.isGif()) {
        mSize.setVisibility(View.VISIBLE);
        mSize.setText(PhotoMetadataUtils.getSizeInMB(item.size) + "M");
    } else {
        mSize.setVisibility(View.GONE);
    }
}
 
Example 28
Source Project: Matisse   Source File: PreviewItemFragment.java    License: Apache License 2.0 5 votes vote down vote up
public static PreviewItemFragment newInstance(Item item) {
    PreviewItemFragment fragment = new PreviewItemFragment();
    Bundle bundle = new Bundle();
    bundle.putParcelable(ARGS_ITEM, item);
    fragment.setArguments(bundle);
    return fragment;
}
 
Example 29
Source Project: Matisse   Source File: PreviewItemFragment.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final Item item = getArguments().getParcelable(ARGS_ITEM);
    if (item == null) {
        return;
    }

    View videoPlayButton = view.findViewById(R.id.video_play_button);
    if (item.isVideo()) {
        videoPlayButton.setVisibility(View.VISIBLE);
        videoPlayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(item.uri, "video/*");
                try {
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(getContext(), R.string.error_no_video_activity, Toast.LENGTH_SHORT).show();
                }
            }
        });
    } else {
        videoPlayButton.setVisibility(View.GONE);
    }

    ImageViewTouch image = (ImageViewTouch) view.findViewById(R.id.image_view);
    image.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);

    Point size = PhotoMetadataUtils.getBitmapSize(item.getContentUri(), getActivity());
    if (item.isGif()) {
        SelectionSpec.getInstance().imageEngine.loadGifImage(getContext(), size.x, size.y, image,
                item.getContentUri());
    } else {
        SelectionSpec.getInstance().imageEngine.loadImage(getContext(), size.x, size.y, image,
                item.getContentUri());
    }
}
 
Example 30
Source Project: Matisse   Source File: Filter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Whether an {@link Item} need filtering.
 */
protected boolean needFiltering(Context context, Item item) {
    for (MimeType type : constraintTypes()) {
        if (type.checkType(context.getContentResolver(), item.getContentUri())) {
            return true;
        }
    }
    return false;
}