blob: c2ed3b4fc8453e4309c473b9e7dc6da9ccf70cbd [file] [log] [blame]
Walter Jang0e72ce92015-02-23 12:27:21 -08001/*
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
17package com.android.contacts.editor;
18
19import com.android.contacts.R;
20import com.android.contacts.editor.PhotoActionPopup.ChoiceListItem;
21
Walter Jang31a74ad2015-10-02 19:17:39 -070022import android.app.Activity;
Walter Jang0e72ce92015-02-23 12:27:21 -080023import android.app.AlertDialog;
24import android.app.Dialog;
25import android.app.DialogFragment;
26import android.content.DialogInterface;
27import android.content.DialogInterface.OnClickListener;
28import android.os.Bundle;
guanxiongliuf34bd422016-07-29 20:24:00 -070029import android.view.View;
30import android.widget.TextView;
Walter Jang0e72ce92015-02-23 12:27:21 -080031
32import java.util.ArrayList;
33
34/**
35 * Displays the options for changing the contact photo.
36 */
37public 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
Walter Jang31a74ad2015-10-02 19:17:39 -070050 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 }
Walter Jang0e72ce92015-02-23 12:27:21 -080055 final Bundle args = new Bundle();
56 args.putInt(ARG_PHOTO_MODE, photoMode);
57
58 PhotoSourceDialogFragment dialog = new PhotoSourceDialogFragment();
Walter Jang0e72ce92015-02-23 12:27:21 -080059 dialog.setArguments(args);
Walter Jang31a74ad2015-10-02 19:17:39 -070060 dialog.show(activity.getFragmentManager(), "photoSource");
Walter Jang0e72ce92015-02-23 12:27:21 -080061 }
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) {
Walter Jang31a74ad2015-10-02 19:17:39 -070078 final Listener listener = (Listener) getActivity();
Walter Jang0e72ce92015-02-23 12:27:21 -080079 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
guanxiongliuf34bd422016-07-29 20:24:00 -070096 final TextView title = (TextView) View.inflate(getActivity(), R.layout.dialog_title, null);
97 title.setText(R.string.menu_change_photo);
Walter Jang0e72ce92015-02-23 12:27:21 -080098 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
guanxiongliuf34bd422016-07-29 20:24:00 -070099 builder.setCustomTitle(title);
Walter Jang0e72ce92015-02-23 12:27:21 -0800100 builder.setItems(items, clickListener);
101 builder.setNegativeButton(android.R.string.cancel, /* listener =*/ null);
102 return builder.create();
103 }
104}