OSDN Git Service

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