Home | History | Annotate | Download | only in editor
      1 /*
      2  * Copyright (C) 2015 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.contacts.editor;
     18 
     19 import android.app.Activity;
     20 import android.app.Dialog;
     21 import android.app.DialogFragment;
     22 import android.content.DialogInterface;
     23 import android.content.DialogInterface.OnClickListener;
     24 import android.os.Bundle;
     25 import android.support.v7.app.AlertDialog;
     26 import android.view.View;
     27 import android.widget.TextView;
     28 
     29 import com.android.contacts.R;
     30 import com.android.contacts.editor.PhotoActionPopup.ChoiceListItem;
     31 
     32 import java.util.ArrayList;
     33 
     34 /**
     35  * Displays the options for changing the contact photo.
     36  */
     37 public class PhotoSourceDialogFragment extends DialogFragment {
     38 
     39     private static final String ARG_PHOTO_MODE = "photoMode";
     40 
     41     /**
     42      * Callbacks for the host of the {@link PhotoSourceDialogFragment}.
     43      */
     44     public interface Listener {
     45         void onRemovePictureChosen();
     46         void onTakePhotoChosen();
     47         void onPickFromGalleryChosen();
     48     }
     49 
     50     public static void show(Activity activity, int photoMode) {
     51         if (!(activity instanceof Listener)) {
     52             throw new IllegalArgumentException(
     53                     "Activity must implement " + Listener.class.getName());
     54         }
     55         final Bundle args = new Bundle();
     56         args.putInt(ARG_PHOTO_MODE, photoMode);
     57 
     58         PhotoSourceDialogFragment dialog = new PhotoSourceDialogFragment();
     59         dialog.setArguments(args);
     60         dialog.show(activity.getFragmentManager(), "photoSource");
     61     }
     62 
     63     @Override
     64     public Dialog onCreateDialog(Bundle savedInstanceState) {
     65         // Get the available options for changing the photo
     66         final int photoMode = getArguments().getInt(ARG_PHOTO_MODE);
     67         final ArrayList<ChoiceListItem> choices =
     68                 PhotoActionPopup.getChoices(getActivity(), photoMode);
     69 
     70         // Prepare the AlertDialog items and click listener
     71         final CharSequence[] items = new CharSequence[choices.size()];
     72         for (int i = 0; i < items.length; i++) {
     73             items[i] = choices.get(i).toString();
     74         }
     75         final OnClickListener clickListener = new OnClickListener() {
     76             @Override
     77             public void onClick(DialogInterface dialogInterface, int which) {
     78                 final Listener listener = (Listener) getActivity();
     79                 final ChoiceListItem choice = choices.get(which);
     80                 switch (choice.getId()) {
     81                     case ChoiceListItem.ID_REMOVE:
     82                         listener.onRemovePictureChosen();
     83                         break;
     84                     case ChoiceListItem.ID_TAKE_PHOTO:
     85                         listener.onTakePhotoChosen();
     86                         break;
     87                     case ChoiceListItem.ID_PICK_PHOTO:
     88                         listener.onPickFromGalleryChosen();
     89                         break;
     90                 }
     91                 dismiss();
     92             }
     93         };
     94 
     95         // Build the AlertDialog
     96         final TextView title = (TextView) View.inflate(getActivity(), R.layout.dialog_title, null);
     97         title.setText(R.string.menu_change_photo);
     98         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     99         builder.setCustomTitle(title);
    100         builder.setItems(items, clickListener);
    101         builder.setNegativeButton(android.R.string.cancel, /* listener =*/ null);
    102         return builder.create();
    103     }
    104 }
    105