diff options
author | Tomasz Mikolajewski <mtomasz@google.com> | 2015-04-15 04:02:13 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-04-15 04:02:14 +0000 |
commit | 3b5adaaca1653969cb12befc5f2a6dda31c90605 (patch) | |
tree | 92652db059df5f151dfb497bb01eae3718aff63e /packages/DocumentsUI/src | |
parent | 738ed14290c06a9c19da4b61b5b57dad1b4208aa (diff) | |
parent | 61686593fd32babb6e86754ea5bfa6bc95cd3690 (diff) | |
download | frameworks_base-3b5adaaca1653969cb12befc5f2a6dda31c90605.zip frameworks_base-3b5adaaca1653969cb12befc5f2a6dda31c90605.tar.gz frameworks_base-3b5adaaca1653969cb12befc5f2a6dda31c90605.tar.bz2 |
Merge "Add an initial version of the copy failure dialog to DocumentsUI."
Diffstat (limited to 'packages/DocumentsUI/src')
3 files changed, 133 insertions, 4 deletions
diff --git a/packages/DocumentsUI/src/com/android/documentsui/CopyService.java b/packages/DocumentsUI/src/com/android/documentsui/CopyService.java index e140f33..337b862 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/CopyService.java +++ b/packages/DocumentsUI/src/com/android/documentsui/CopyService.java @@ -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. @@ -158,13 +178,14 @@ public class CopyService extends IntentService { final Context context = getApplicationContext(); final Intent navigateIntent = new Intent(context, StandaloneActivity.class); - navigateIntent.putExtra(EXTRA_STACK, (Parcelable)stack); + navigateIntent.putExtra(EXTRA_STACK, (Parcelable) stack); mProgressBuilder = new Notification.Builder(this) .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); diff --git a/packages/DocumentsUI/src/com/android/documentsui/FailureDialogFragment.java b/packages/DocumentsUI/src/com/android/documentsui/FailureDialogFragment.java new file mode 100644 index 0000000..1748c9c --- /dev/null +++ b/packages/DocumentsUI/src/com/android/documentsui/FailureDialogFragment.java @@ -0,0 +1,99 @@ +/* + * 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("• %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(); + } +} diff --git a/packages/DocumentsUI/src/com/android/documentsui/StandaloneActivity.java b/packages/DocumentsUI/src/com/android/documentsui/StandaloneActivity.java index f542838..976f21d 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/StandaloneActivity.java +++ b/packages/DocumentsUI/src/com/android/documentsui/StandaloneActivity.java @@ -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); } |