예제 #1
0
  private void onUrisDecoded(@Nullable List<Upload> uploads) {
    if (uploads == null || uploads.isEmpty()) {
      boolean isEmpty = mAdapter == null || mAdapter.isEmpty();
      mMultiView.setViewState(
          isEmpty ? MultiStateView.VIEW_STATE_EMPTY : MultiStateView.VIEW_STATE_CONTENT);
    } else {
      if (mAdapter == null) {
        mAdapter = new UploadPhotoAdapter(getActivity(), uploads, this);
        mRecyclerView.setAdapter(mAdapter);
      } else {
        mAdapter.addItems(uploads);
      }

      mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
    }

    mPhotoUris = null;

    if (mListener != null) {
      if (mAdapter == null || mAdapter.isEmpty()) {
        mListener.onPhotoRemoved(0);
      } else {
        mListener.onPhotoAdded();
      }
    }
  }
예제 #2
0
  private void handleArgs(@Nullable Bundle args) {
    if (args == null) {
      mMultiView.setViewState(MultiStateView.VIEW_STATE_EMPTY);
      return;
    }

    List<Upload> uploads = null;

    if (args.containsKey(KEY_PASSED_FILE)) {
      LogUtil.v(TAG, "Received file from bundle");
      uploads = new ArrayList<>(1);
      uploads.add(new Upload(args.getString(KEY_PASSED_FILE)));
    } else if (args.containsKey(KEY_PASSED_LINK)) {
      mMultiView.setViewState(MultiStateView.VIEW_STATE_EMPTY);
      String link = args.getString(KEY_PASSED_LINK);
      getChildFragmentManager()
          .beginTransaction()
          .add(UploadLinkDialogFragment.newInstance(link), UploadLinkDialogFragment.TAG)
          .commit();
    } else if (args.containsKey(KEY_PASSED_URIS)) {
      ArrayList<Uri> photoUris = args.getParcelableArrayList(KEY_PASSED_URIS);

      if (photoUris != null && !photoUris.isEmpty()) {
        LogUtil.v(TAG, "Received " + photoUris.size() + " images via Share intent");
        new DecodeImagesTask(this).execute(photoUris);
        mMultiView.setViewState(MultiStateView.VIEW_STATE_LOADING);
      }
    }

    if (uploads != null && !uploads.isEmpty()) {
      mAdapter = new UploadPhotoAdapter(getActivity(), uploads, this);
      mRecyclerView.setAdapter(mAdapter);
      mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
    }
  }
예제 #3
0
  public void onLinkAdded(String link) {
    Upload upload = new Upload(link, true);

    if (mAdapter == null) {
      mAdapter = new UploadPhotoAdapter(getActivity(), upload, this);
      mRecyclerView.setAdapter(mAdapter);
    } else {
      mAdapter.addItem(upload);
    }

    mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
    if (mListener != null) mListener.onPhotoAdded();
  }
예제 #4
0
  private void onNeedsReadPermission(List<Uri> photoUris) {
    mMultiView.setViewState(
        mAdapter != null && !mAdapter.isEmpty()
            ? MultiStateView.VIEW_STATE_CONTENT
            : MultiStateView.VIEW_STATE_EMPTY);
    mPhotoUris = photoUris;
    int level = PermissionUtils.getPermissionLevel(this, Manifest.permission.READ_EXTERNAL_STORAGE);

    switch (level) {
        // Should never have gotten here if its available
      case PermissionUtils.PERMISSION_AVAILABLE:
        new DecodeImagesTask(this).doInBackground(mPhotoUris);
        break;

      case PermissionUtils.PERMISSION_DENIED:
        int uriSize = photoUris != null ? photoUris.size() : 0;
        new AlertDialog.Builder(getActivity(), theme.getAlertDialogTheme())
            .setTitle(R.string.permission_title)
            .setMessage(
                getResources()
                    .getQuantityString(R.plurals.permission_rational_file_access, uriSize, uriSize))
            .setNegativeButton(R.string.cancel, null)
            .setPositiveButton(
                R.string.okay,
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                    ActivityCompat.requestPermissions(
                        getActivity(),
                        new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
                        RequestCodes.REQUEST_PERMISSION_READ);
                  }
                })
            .show();
        break;

      case PermissionUtils.PERMISSION_NEVER_ASKED:
        ActivityCompat.requestPermissions(
            getActivity(),
            new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
            RequestCodes.REQUEST_PERMISSION_READ);
        break;
    }
  }
예제 #5
0
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Resources res = getResources();
    ViewUtils.setRecyclerViewGridDefaults(
        getActivity(),
        mRecyclerView,
        res.getInteger(R.integer.upload_num_columns),
        res.getDimensionPixelSize(R.dimen.grid_padding));
    ViewUtils.setEmptyText(mMultiView, R.id.emptyMessage, R.string.upload_empty_message);
    new ItemTouchHelper(mSimpleItemTouchCallback).attachToRecyclerView(mRecyclerView);

    if (savedInstanceState != null && savedInstanceState.containsKey(KEY_SAVED_ITEMS)) {
      List<Upload> uploads = savedInstanceState.getParcelableArrayList(KEY_SAVED_ITEMS);
      mAdapter = new UploadPhotoAdapter(getActivity(), uploads, this);
      mRecyclerView.setAdapter(mAdapter);
      mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
      if (mListener != null) mListener.onPhotoAdded();
    } else {
      handleArgs(getArguments());
    }
  }
예제 #6
0
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
      case RequestCodes.TAKE_PHOTO:
        if (resultCode == Activity.RESULT_OK) {
          if (!FileUtil.isFileValid(mTempFile)) {
            Snackbar.make(mMultiView, R.string.upload_camera_error, Snackbar.LENGTH_LONG).show();
            return;
          }

          FileUtil.scanFile(Uri.fromFile(mTempFile), getActivity());
          String fileLocation = mTempFile.getAbsolutePath();
          Upload upload = new Upload(fileLocation);

          if (mAdapter == null) {
            mAdapter = new UploadPhotoAdapter(getActivity(), upload, this);
            mRecyclerView.setAdapter(mAdapter);
          } else {
            mAdapter.addItem(upload);
          }

          mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
          mTempFile = null;
        }
        break;

      case RequestCodes.SELECT_PHOTO:
        if (resultCode == Activity.RESULT_OK && data != null) {
          ClipData clipData = data.getClipData();
          List<Uri> uris = null;

          // Check if we have multiple images
          if (clipData != null) {
            int size = clipData.getItemCount();
            uris = new ArrayList<>(size);

            for (int i = 0; i < size; i++) {
              uris.add(clipData.getItemAt(i).getUri());
            }
          } else if (data.getData() != null) {
            // If not multiple images, then only one was selected
            uris = new ArrayList<>(1);
            uris.add(data.getData());
          }

          if (uris != null && !uris.isEmpty()) {
            mMultiView.setViewState(MultiStateView.VIEW_STATE_LOADING);
            new DecodeImagesTask(this).execute(uris);
          } else {
            Snackbar.make(mMultiView, R.string.error_generic, Snackbar.LENGTH_LONG).show();
          }
        }
        break;

      case RequestCodes.UPLOAD_EDIT:
        if (resultCode == Activity.RESULT_OK && data != null) {
          final Upload upload;

          if (data.hasExtra(UploadEditActivity.KEY_UPDATED_UPLOAD)) {
            upload = data.getParcelableExtra(UploadEditActivity.KEY_UPDATED_UPLOAD);

            if (upload != null) {
              mAdapter.updateItem(upload);
            }
          } else if (data.hasExtra(UploadEditActivity.KEY_UPDATED_DELETED)) {
            upload = data.getParcelableExtra(UploadEditActivity.KEY_UPDATED_DELETED);

            if (upload != null) {
              final int itemIndex = mAdapter.indexOf(upload);

              if (itemIndex > -1) {
                mAdapter.removeItem(itemIndex);
                if (mListener != null) mListener.onPhotoRemoved(mAdapter.getItemCount());
                if (mAdapter.isEmpty()) mMultiView.setViewState(MultiStateView.VIEW_STATE_EMPTY);

                Snackbar.make(mMultiView, R.string.upload_photo_removed, Snackbar.LENGTH_LONG)
                    .setAction(
                        R.string.undo,
                        new View.OnClickListener() {
                          @Override
                          public void onClick(View v) {
                            mMultiView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                            mAdapter.addItem(upload, itemIndex);
                            if (mListener != null) mListener.onPhotoAdded();
                          }
                        })
                    .show();
              }
            }
          }
        }
        break;
    }

    super.onActivityResult(requestCode, resultCode, data);
  }