• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.wallpaper.livepicker;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.Dialog;
22 import android.app.DialogFragment;
23 import android.app.Fragment;
24 import android.app.WallpaperInfo;
25 import android.os.Bundle;
26 import android.content.DialogInterface;
27 import android.content.Intent;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.AdapterView;
33 import android.widget.BaseAdapter;
34 
35 public class LiveWallpaperActivity extends Activity {
36     private static final String LOG_TAG = "LiveWallpapersPicker";
37     private static final int REQUEST_PREVIEW = 100;
38 
39     @Override
onCreate(Bundle savedInstanceState)40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         setContentView(R.layout.live_wallpaper_base);
43 
44         Fragment fragmentView = getFragmentManager().findFragmentById(R.id.live_wallpaper_fragment);
45         if (fragmentView == null) {
46             /* When the screen is XLarge, the fragment is not included in the layout, so show it
47              * as a dialog
48              */
49             DialogFragment fragment = WallpaperDialog.newInstance();
50             fragment.show(getFragmentManager(), "dialog");
51         }
52     }
53 
54     @Override
onActivityResult(int requestCode, int resultCode, Intent data)55     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
56         super.onActivityResult(requestCode, resultCode, data);
57 
58         if (requestCode == REQUEST_PREVIEW) {
59             if (resultCode == RESULT_OK) finish();
60         }
61     }
62 
63     public static class WallpaperDialog extends DialogFragment implements
64             AdapterView.OnItemClickListener{
65         private static final String EMBEDDED_KEY = "com.android.wallpaper.livepicker."
66                 + "LiveWallpaperActivity$WallpaperDialog.EMBEDDED_KEY";
67         private LiveWallpaperListAdapter mAdapter;
68         private boolean mEmbedded;
69 
newInstance()70         public static WallpaperDialog newInstance() {
71             WallpaperDialog dialog = new WallpaperDialog();
72             dialog.setCancelable(true);
73             return dialog;
74         }
75 
76         @Override
onCreate(Bundle savedInstanceState)77         public void onCreate(Bundle savedInstanceState) {
78             super.onCreate(savedInstanceState);
79             if (savedInstanceState != null && savedInstanceState.containsKey(EMBEDDED_KEY)) {
80                 mEmbedded = savedInstanceState.getBoolean(EMBEDDED_KEY);
81             } else {
82                 mEmbedded = isInLayout();
83             }
84         }
85 
86         @Override
onSaveInstanceState(Bundle outState)87         public void onSaveInstanceState(Bundle outState) {
88             outState.putBoolean(EMBEDDED_KEY, mEmbedded);
89         }
90 
91         @Override
onDismiss(DialogInterface dialog)92         public void onDismiss(DialogInterface dialog) {
93             /* On orientation changes, the dialog is effectively "dismissed" so this is called
94              * when the activity is no longer associated with this dying dialog fragment. We
95              * should just safely ignore this case by checking if getActivity() returns null
96              */
97             Activity activity = getActivity();
98             if (activity != null) {
99                 activity.finish();
100             }
101         }
102 
103         @Override
onCreateDialog(Bundle savedInstanceState)104         public Dialog onCreateDialog(Bundle savedInstanceState) {
105             final int contentInset = getResources().getDimensionPixelSize(
106                     R.dimen.dialog_content_inset);
107             View view = generateView(getActivity().getLayoutInflater(), null);
108 
109             AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
110             builder.setNegativeButton(R.string.wallpaper_cancel, null);
111             builder.setTitle(R.string.live_wallpaper_picker_title);
112             builder.setView(view, contentInset, contentInset, contentInset, contentInset);
113             return builder.create();
114         }
115 
116         @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)117         public View onCreateView(LayoutInflater inflater, ViewGroup container,
118                 Bundle savedInstanceState) {
119             /* If this fragment is embedded in the layout of this activity, then we should
120              * generate a view to display. Otherwise, a dialog will be created in
121              * onCreateDialog()
122              */
123             if (mEmbedded) {
124                 return generateView(inflater, container);
125             }
126             return null;
127         }
128 
129         @SuppressWarnings("unchecked")
generateView(LayoutInflater inflater, ViewGroup container)130         private View generateView(LayoutInflater inflater, ViewGroup container) {
131             View layout = inflater.inflate(R.layout.live_wallpaper_list, container, false);
132 
133             mAdapter = new LiveWallpaperListAdapter(getActivity());
134             AdapterView<BaseAdapter> adapterView =
135                     (AdapterView<BaseAdapter>) layout.findViewById(android.R.id.list);
136             adapterView.setAdapter(mAdapter);
137             adapterView.setOnItemClickListener(this);
138             adapterView.setEmptyView(layout.findViewById(android.R.id.empty));
139             return layout;
140         }
141 
onItemClick(AdapterView<?> parent, View view, int position, long id)142         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
143             LiveWallpaperListAdapter.LiveWallpaperInfo wallpaperInfo =
144                     (LiveWallpaperListAdapter.LiveWallpaperInfo) mAdapter.getItem(position);
145             final Intent intent = wallpaperInfo.intent;
146             final WallpaperInfo info = wallpaperInfo.info;
147             LiveWallpaperPreview.showPreview(getActivity(), REQUEST_PREVIEW, intent, info);
148         }
149     }
150 }
151