-
Notifications
You must be signed in to change notification settings - Fork 33
/
Wallpaper.java
232 lines (197 loc) · 7.76 KB
/
Wallpaper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright (C) 2012 ParanoidAndroid Project
*
* Licensed under the GNU GPLv2 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl-2.0.txt
*/
package com.paranoid.ParanoidWallpapers;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.io.IOException;
import java.util.ArrayList;
@SuppressLint("ParserError")
public class Wallpaper extends FragmentActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
* sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
* keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
* to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
/**
* Menu item used for "Apply" button on actionbar
*/
private static final int MENU_APPLY = Menu.FIRST;
/**
* The {@link Integer} that stores current fragment selected
*/
private int mCurrentFragment;
/**
* The {@link ArrayList} that will host the wallpapers resource ID's
*/
static ArrayList <Integer> sWallpapers = new ArrayList<Integer>();
/**
* The {@link String[]} that will store wallpaper name
*/
String[] mWallpaperInfo;
/**
* The {@link Context} to be used by the app
*/
static Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new SimpleOnPageChangeListener(){
public void onPageSelected(int position) {
mCurrentFragment = position;
}
});
sWallpapers.clear();
final Resources resources = getResources();
final String packageName = getApplication().getPackageName();
fetchWallpapers(resources, packageName, R.array.wallpapers);
mWallpaperInfo = resources.getStringArray(R.array.info);
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
* sections of the app.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new WallpaperFragment();
Bundle args = new Bundle();
args.putInt(WallpaperFragment.ARG_SECTION_NUMBER, i);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return sWallpapers.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mWallpaperInfo[position];
}
}
public static class WallpaperFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContext = getActivity();
Bundle args = getArguments();
LinearLayout holder = new LinearLayout(mContext);
holder.setLayoutParams(new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
ImageView img = new ImageView(mContext);
img.setLayoutParams(new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
img.setImageResource(sWallpapers.get(args.getInt(ARG_SECTION_NUMBER)));
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.addView(img);
return holder;
}
}
private void fetchWallpapers(Resources resources, String packageName, int list) {
final String[] extras = resources.getStringArray(list);
for (String extra : extras) {
int res = resources.getIdentifier(extra, "drawable", packageName);
if (res != 0) {
sWallpapers.add(res);
}
}
}
class WallpaperLoader extends AsyncTask<Integer, Void, Boolean> {
BitmapFactory.Options mOptions;
ProgressDialog mDialog;
WallpaperLoader() {
mOptions = new BitmapFactory.Options();
mOptions.inDither = false;
mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
}
@Override
protected Boolean doInBackground(Integer... params) {
try {
Bitmap b = BitmapFactory.decodeResource(getResources(),
sWallpapers.get(params[0]), mOptions);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(mContext);
try {
wallpaperManager.setBitmap(b);
} catch (IOException e) {
// If we crash, we will probably have a null bitmap
// return before recycling to avoid exception
throw new NullPointerException();
}
// Help GC
b.recycle();
return true;
} catch (OutOfMemoryError e) {
return false;
} catch(NullPointerException e){
return false;
}
}
@Override
protected void onPostExecute(Boolean success) {
mDialog.dismiss();
finish();
}
@Override
protected void onPreExecute() {
mDialog = ProgressDialog.show(mContext, null, getString(R.string.applying));
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_APPLY:
new WallpaperLoader().execute(mCurrentFragment);
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, MENU_APPLY, 0, R.string.action_apply)
.setIcon(android.R.drawable.ic_menu_set_as)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
}