OSDN Git Service

Automatic translation import
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / menu / PhotoSelectionDialog.java
1 /*
2  * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6  * or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */
11
12 package com.andrew.apollo.menu;
13
14 import android.app.AlertDialog;
15 import android.app.Dialog;
16 import android.content.DialogInterface;
17 import android.os.Bundle;
18 import android.support.v4.app.DialogFragment;
19 import android.widget.ArrayAdapter;
20 import android.widget.ListAdapter;
21
22 import com.andrew.apollo.Config;
23 import com.andrew.apollo.R;
24 import com.andrew.apollo.ui.activities.ProfileActivity;
25 import com.andrew.apollo.utils.ApolloUtils;
26 import com.andrew.apollo.utils.Lists;
27
28 import java.util.ArrayList;
29
30 /**
31  * Used when the user touches the image in the header in {@link ProfileActivity}
32  * . It provides an easy interface for them to choose a new image, use the old
33  * image, or search Google for one.
34  * 
35  * @author Andrew Neal (andrewdneal@gmail.com)
36  */
37 public class PhotoSelectionDialog extends DialogFragment {
38
39     private static final int NEW_PHOTO = 0;
40
41     private static final int OLD_PHOTO = 1;
42
43     private static final int GOOGLE_SEARCH = 2;
44
45     private static final int FETCH_IMAGE = 3;
46
47     private final ArrayList<String> mChoices = Lists.newArrayList();
48
49     private static ProfileType mProfileType;
50
51     /**
52      * Empty constructor as per the {@link Fragment} documentation
53      */
54     public PhotoSelectionDialog() {
55     }
56
57     /**
58      * @param title The dialog title.
59      * @param value The MIME type
60      * @return A new instance of the dialog.
61      */
62     public static PhotoSelectionDialog newInstance(final String title, final ProfileType type) {
63         final PhotoSelectionDialog frag = new PhotoSelectionDialog();
64         final Bundle args = new Bundle();
65         args.putString(Config.NAME, title);
66         frag.setArguments(args);
67         mProfileType = type;
68         return frag;
69     }
70
71     /**
72      * {@inheritDoc}
73      */
74     @Override
75     public Dialog onCreateDialog(final Bundle savedInstanceState) {
76         final String title = getArguments().getString(Config.NAME);
77         switch (mProfileType) {
78             case ARTIST:
79                 setArtistChoices();
80                 break;
81             case ALBUM:
82                 setAlbumChoices();
83                 break;
84             case OTHER:
85                 setOtherChoices();
86                 break;
87             default:
88                 break;
89         }
90         // Dialog item Adapter
91         final ProfileActivity activity = (ProfileActivity) getActivity();
92         final ListAdapter adapter = new ArrayAdapter<String>(activity,
93                 android.R.layout.select_dialog_item, mChoices);
94         return new AlertDialog.Builder(activity).setTitle(title)
95                 .setAdapter(adapter, new DialogInterface.OnClickListener() {
96
97                     @Override
98                     public void onClick(final DialogInterface dialog, final int which) {
99                         switch (which) {
100                             case NEW_PHOTO:
101                                 activity.selectNewPhoto();
102                                 break;
103                             case OLD_PHOTO:
104                                 activity.selectOldPhoto();
105                                 break;
106                             case FETCH_IMAGE:
107                                 activity.fetchAlbumArt();
108                                 break;
109                             case GOOGLE_SEARCH:
110                                 activity.googleSearch();
111                                 break;
112                             default:
113                                 break;
114                         }
115                     }
116                 }).create();
117     }
118
119     /**
120      * Adds the choices for the artist profile image.
121      */
122     private void setArtistChoices() {
123         // Select a photo from the gallery
124         mChoices.add(NEW_PHOTO, getString(R.string.new_photo));
125         if (ApolloUtils.isOnline(getActivity())) {
126             // Option to fetch the old artist image
127             mChoices.add(OLD_PHOTO, getString(R.string.context_menu_fetch_artist_image));
128             // Search Google for the artist name
129             mChoices.add(GOOGLE_SEARCH, getString(R.string.google_search));
130         }
131     }
132
133     /**
134      * Adds the choices for the album profile image.
135      */
136     private void setAlbumChoices() {
137         // Select a photo from the gallery
138         mChoices.add(NEW_PHOTO, getString(R.string.new_photo));
139         // Option to fetch the old album image
140         mChoices.add(OLD_PHOTO, getString(R.string.old_photo));
141         if (ApolloUtils.isOnline(getActivity())) {
142             // Search Google for the album name
143             mChoices.add(GOOGLE_SEARCH, getString(R.string.google_search));
144             // Option to fetch the album image
145             mChoices.add(FETCH_IMAGE, getString(R.string.context_menu_fetch_album_art));
146         }
147     }
148
149     /**
150      * Adds the choices for the genre and playlist images.
151      */
152     private void setOtherChoices() {
153         // Select a photo from the gallery
154         mChoices.add(NEW_PHOTO, getString(R.string.new_photo));
155         // Option to use the default image
156         mChoices.add(OLD_PHOTO, getString(R.string.use_default));
157     }
158
159     /**
160      * Easily detect the MIME type
161      */
162     public enum ProfileType {
163         ARTIST, ALBUM, OTHER
164     }
165 }