Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3b5adaac authored by Tomasz Mikolajewski's avatar Tomasz Mikolajewski Committed by Android (Google) Code Review
Browse files

Merge "Add an initial version of the copy failure dialog to DocumentsUI."

parents 738ed142 61686593
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -126,5 +126,17 @@
    </plurals>
    <!-- Text shown on the copy notification while DocumentsUI performs setup in preparation for copying files [CHAR LIMIT=32] -->
    <string name="copy_preparing">Preparing for copy\u2026</string>

    <!-- Title of the copy error notification [CHAR LIMIT=48] -->
    <plurals name="copy_error_notification_title">
      <item quantity="one">Error copying <xliff:g id="count" example="1">%1$d</xliff:g> file.</item>
      <item quantity="other">Error copying <xliff:g id="count" example="1">%1$d</xliff:g> files.</item>
    </plurals>
    <!-- Second line for notifications saying that more information will be shown after touching [CHAR LIMIT=48] -->
    <string name="notification_touch_for_details">Touch to view details</string>
    <!-- Label of a dialog button for retrying a failed operation [CHAR LIMIT=24] -->
    <string name="retry">Retry</string>
    <!-- Title of the copying failure alert dialog. [CHAR LIMIT=48] -->
    <string name="copy_failure_alert_title">Error copying files</string>
    <!-- Contents of the copying failure alert dialog. [CHAR LIMIT=48] -->
    <string name="copy_failure_alert_content">Following files are not copied: <xliff:g id="list">%1$s</xliff:g></string>
</resources>
+25 −4
Original line number Diff line number Diff line
@@ -57,6 +57,10 @@ public class CopyService extends IntentService {
    private static final String EXTRA_CANCEL = "com.android.documentsui.CANCEL";
    public static final String EXTRA_SRC_LIST = "com.android.documentsui.SRC_LIST";
    public static final String EXTRA_STACK = "com.android.documentsui.STACK";
    public static final String EXTRA_FAILURE = "com.android.documentsui.FAILURE";

    // TODO: Move it to a shared file when more operations are implemented.
    public static final int FAILURE_COPY = 1;

    private NotificationManager mNotificationManager;
    private Notification.Builder mProgressBuilder;
@@ -66,7 +70,7 @@ public class CopyService extends IntentService {
    private volatile boolean mIsCancelled;
    // Parameters of the copy job. Requests to an IntentService are serialized so this code only
    // needs to deal with one job at a time.
    private final List<Uri> mFailedFiles;
    private final ArrayList<Uri> mFailedFiles;
    private long mBatchSize;
    private long mBytesCopied;
    private long mStartTime;
@@ -128,7 +132,23 @@ public class CopyService extends IntentService {
            mNotificationManager.cancel(mJobId, 0);

            if (mFailedFiles.size() > 0) {
                // TODO: Display a notification when an error has occurred.
                final Context context = getApplicationContext();
                final Intent navigateIntent = new Intent(context, StandaloneActivity.class);
                navigateIntent.putExtra(EXTRA_STACK, (Parcelable) stack);
                navigateIntent.putExtra(EXTRA_FAILURE, FAILURE_COPY);
                navigateIntent.putParcelableArrayListExtra(EXTRA_SRC_LIST, mFailedFiles);

                final Notification.Builder errorBuilder = new Notification.Builder(this)
                        .setContentTitle(context.getResources().
                                getQuantityString(R.plurals.copy_error_notification_title,
                                        mFailedFiles.size(), mFailedFiles.size()))
                        .setContentText(getString(R.string.notification_touch_for_details))
                        .setContentIntent(PendingIntent.getActivity(context, 0, navigateIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT))
                        .setCategory(Notification.CATEGORY_ERROR)
                        .setSmallIcon(R.drawable.ic_menu_copy)
                        .setAutoCancel(true);
                mNotificationManager.notify(mJobId, 0, errorBuilder.build());
            }

            // TODO: Display a toast if the copy was cancelled.
@@ -164,7 +184,8 @@ public class CopyService extends IntentService {
                .setContentTitle(getString(R.string.copy_notification_title))
                .setContentIntent(PendingIntent.getActivity(context, 0, navigateIntent, 0))
                .setCategory(Notification.CATEGORY_PROGRESS)
                .setSmallIcon(R.drawable.ic_menu_copy).setOngoing(true);
                .setSmallIcon(R.drawable.ic_menu_copy)
                .setOngoing(true);

        final Intent cancelIntent = new Intent(this, CopyService.class);
        cancelIntent.putExtra(EXTRA_CANCEL, mJobId);
+99 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.documentsui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;

import com.android.documentsui.model.DocumentInfo;

import java.io.FileNotFoundException;
import java.util.ArrayList;

/**
 * Alert dialog for failed operations.
 */
public class FailureDialogFragment extends DialogFragment
        implements DialogInterface.OnClickListener {
    private static final String TAG = "FailureDialogFragment";

    private int mFailure;
    private ArrayList<Uri> mFailedSrcList;

    public static void show(FragmentManager fm, int failure, ArrayList<Uri> failedSrcList) {
        // TODO: Add support for other failures than copy.
        if (failure != CopyService.FAILURE_COPY) {
            return;
        }

        final Bundle args = new Bundle();
        args.putInt(CopyService.EXTRA_FAILURE, failure);
        args.putParcelableArrayList(CopyService.EXTRA_SRC_LIST, failedSrcList);

        final FragmentTransaction ft = fm.beginTransaction();
        final FailureDialogFragment fragment = new FailureDialogFragment();
        fragment.setArguments(args);

        ft.add(fragment, TAG);
        ft.commitAllowingStateLoss();
    }

    @Override
    public void onClick(DialogInterface dialog, int whichButton) {
      // TODO: Pass mFailure and mFailedSrcList to the parent fragment.
    }

    @Override
    public Dialog onCreateDialog(Bundle inState) {
        super.onCreate(inState);

        mFailure = getArguments().getInt(CopyService.EXTRA_FAILURE);
        mFailedSrcList = getArguments().getParcelableArrayList(CopyService.EXTRA_SRC_LIST);

        final StringBuilder list = new StringBuilder("<p>");
        for (Uri documentUri : mFailedSrcList) {
            try {
                final DocumentInfo documentInfo = DocumentInfo.fromUri(
                    getActivity().getContentResolver(), documentUri);
                list.append(String.format("&#8226; %s<br>", documentInfo.displayName));
            }
            catch (FileNotFoundException ignore) {
                // Source file most probably gone.
            }
        }
        list.append("</p>");
        final String message = String.format(getString(R.string.copy_failure_alert_content),
                list.toString());

        return new AlertDialog.Builder(getActivity())
            .setTitle(getString(R.string.copy_failure_alert_title))
            .setMessage(Html.fromHtml(message))
            // TODO: Implement retrying the copy operation.
            .setPositiveButton(R.string.retry, this)
            .setNegativeButton(android.R.string.cancel, this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .create();
    }
}
+9 −0
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.android.documentsui.FailureDialogFragment;
import com.android.documentsui.RecentsProvider.ResumeColumns;
import com.android.documentsui.model.DocumentInfo;
import com.android.documentsui.model.DocumentStack;
@@ -73,6 +74,7 @@ import libcore.io.IoUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
@@ -153,6 +155,13 @@ public class StandaloneActivity extends BaseActivity {
        RootsFragment.show(getFragmentManager(), null);
        if (!mState.restored) {
            new RestoreStackTask().execute();
            final Intent intent = getIntent();
            final int failure = intent.getIntExtra(CopyService.EXTRA_FAILURE, 0);
            if (failure != 0) {
                final ArrayList<Uri> failedSrcList = intent.getParcelableArrayListExtra(
                        CopyService.EXTRA_SRC_LIST);
                FailureDialogFragment.show(getFragmentManager(), failure, failedSrcList);
            }
        } else {
            onCurrentDirectoryChanged(ANIM_NONE);
        }