Home | History | Annotate | Download | only in picker
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.documentsui.picker;
     18 
     19 import android.app.AlertDialog;
     20 import android.app.Dialog;
     21 import android.app.DialogFragment;
     22 import android.app.Fragment;
     23 import android.app.FragmentManager;
     24 import android.app.FragmentTransaction;
     25 import android.content.DialogInterface;
     26 import android.os.Bundle;
     27 
     28 import com.android.documentsui.R;
     29 import com.android.documentsui.base.DocumentInfo;
     30 import com.android.documentsui.base.Shared;
     31 
     32 /**
     33  * Used to confirm with user that it's OK to overwrite an existing file.
     34  */
     35 public class OverwriteConfirmFragment extends DialogFragment {
     36 
     37     private static final String TAG = "OverwriteConfirmFragment";
     38 
     39     private ActionHandler<PickActivity> mActions;
     40     private DocumentInfo mOverwriteTarget;
     41 
     42     @Override
     43     public void onActivityCreated(Bundle savedInstanceState) {
     44         super.onActivityCreated(savedInstanceState);
     45 
     46         mActions = ((PickActivity) getActivity()).getInjector().actions;
     47     }
     48 
     49     @Override
     50     public Dialog onCreateDialog(Bundle savedInstanceState) {
     51         Bundle arg = (getArguments() != null) ? getArguments() : savedInstanceState;
     52 
     53         mOverwriteTarget = arg.getParcelable(Shared.EXTRA_DOC);
     54 
     55         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     56         final String message = String.format(
     57                 getString(R.string.overwrite_file_confirmation_message),
     58                 mOverwriteTarget.displayName);
     59         builder.setMessage(message);
     60         builder.setPositiveButton(
     61                 android.R.string.ok,
     62                 (DialogInterface dialog, int id) ->
     63                         mActions.finishPicking(mOverwriteTarget.derivedUri));
     64         builder.setNegativeButton(android.R.string.cancel, null);
     65 
     66         return builder.create();
     67     }
     68 
     69     @Override
     70     public void onSaveInstanceState(Bundle outState) {
     71         super.onSaveInstanceState(outState);
     72 
     73         outState.putParcelable(Shared.EXTRA_DOC, mOverwriteTarget);
     74     }
     75 
     76     public static void show(FragmentManager fm, DocumentInfo overwriteTarget) {
     77         Bundle arg = new Bundle();
     78         arg.putParcelable(Shared.EXTRA_DOC, overwriteTarget);
     79 
     80         FragmentTransaction ft = fm.beginTransaction();
     81         Fragment f = new OverwriteConfirmFragment();
     82         f.setArguments(arg);
     83         ft.add(f, TAG);
     84         ft.commitAllowingStateLoss();
     85     }
     86 }
     87