summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/icons/IconPickerActivity.java
blob: 3b49871cbefd35805234785bfe9a12289417d498 (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (C) 2017 The LineageOS Project
 * Copyright (C) 2017 Paranoid Android
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.icons;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.GridLayoutManager.SpanSizeLookup;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.TextView;

import com.android.launcher3.IconCache;
import com.android.launcher3.ItemInfo;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.R;

import java.util.ArrayList;
import java.util.List;

public class IconPickerActivity extends Activity {
    private static final String TAG = "IconPickerActivity";
    public static final String EXTRA_PACKAGE = "app_package";
    public static final String EXTRA_LABEL = "app_label";
    public static final String EXTRA_ICON_PACK = "icon_pack_package";

    private GridLayoutManager mGridLayout;
    private ProgressBar mProgressBar;
    private RecyclerView mIconsGrid;

    private static ItemInfo sItemInfo;
    private IconCache mIconCache;
    private IconsHandler mIconsHandler;

    private List<String> mAllicons;
    private List<String> mMatchingIcons;
    private GridAdapter mAdapter;

    private String mCurrentPackageLabel;
    private String mCurrentPackageName;
    private String mIconPackPackageName;
    private int mIconSize;
    private boolean mCanSearch;

    public static void setItemInfo(ItemInfo info) {
        sItemInfo = info;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_icons_view);

        mCurrentPackageName = getIntent().getStringExtra(EXTRA_PACKAGE);
        mCurrentPackageLabel = getIntent().getStringExtra(EXTRA_LABEL);
        mIconPackPackageName = getIntent().getStringExtra(EXTRA_ICON_PACK);

        mIconCache = LauncherAppState.getInstance(this).getIconCache();
        mIconsHandler = IconCache.getIconsHandler(this);

        int itemSpacing = getResources().getDimensionPixelSize(R.dimen.grid_item_spacing);
        mIconsGrid = (RecyclerView) findViewById(R.id.icons_grid);
        mIconsGrid.setHasFixedSize(true);
        mIconsGrid.addItemDecoration(new GridItemSpacer(itemSpacing));
        mGridLayout = new GridLayoutManager(this, 4);
        mIconsGrid.setLayoutManager(mGridLayout);
        mIconsGrid.setAlpha(0.0f);

        mProgressBar = (ProgressBar) findViewById(R.id.icons_grid_progress);
        mIconSize = getResources().getDimensionPixelSize(R.dimen.icon_pack_icon_size);

        LinearLayout headerBar = (LinearLayout) findViewById(R.id.icons_grid_name_header);
        try {
            PackageManager pm = getPackageManager();
            ApplicationInfo info = pm.getApplicationInfo(mIconPackPackageName,
                    PackageManager.GET_META_DATA);

            TextView headerTitle = (TextView) findViewById(R.id.icons_grid_name_header_title);
            ImageView headerIcon = (ImageView) findViewById(R.id.icons_grid_name_header_icon);

            headerTitle.setText(pm.getApplicationLabel(info));
            headerIcon.setImageDrawable(pm.getApplicationIcon(info));
        } catch (PackageManager.NameNotFoundException e) {
            headerBar.setVisibility(View.GONE);
            Log.e(TAG, e.getMessage());
        }

        new Thread(() -> {
            mCanSearch = false;
            final Activity activity = IconPickerActivity.this;
            mAllicons = mIconsHandler.getAllDrawables(mIconPackPackageName);
            mMatchingIcons = mIconsHandler.getMatchingDrawables(mCurrentPackageName);
            activity.runOnUiThread(() -> {
                mAdapter = new GridAdapter(mAllicons, mMatchingIcons);
                mIconsGrid.setAdapter(mAdapter);
                mProgressBar.setVisibility(View.GONE);
                mIconsGrid.animate().alpha(1.0f);
                mCanSearch = true;
            });
        }).start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.icon_picker, menu);

        MenuItem search = menu.findItem(R.id.icon_picker_search);
        SearchView searchView = (SearchView) search.getActionView();
        searchView.setOnQueryTextListener(getSearchListener());
        return true;
    }

    private SearchView.OnQueryTextListener getSearchListener() {
        return new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                if (!mCanSearch) {
                    return false;
                }
                IconsSearchUtils.filter(s, mMatchingIcons, mAllicons, mAdapter);
                return true;
            }
        };
    }

    class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> implements Filterable {

        private static final int TYPE_MATCHING_HEADER = 0;
        private static final int TYPE_MATCHING_ICONS = 1;
        private static final int TYPE_ALL_HEADER = 2;
        private static final int TYPE_ALL_ICONS = 3;

        private boolean mNoMatchingDrawables;

        private List<String> mAllDrawables = new ArrayList<>();
        private List<String> mMatchingDrawables = new ArrayList<>();
        private final SpanSizeLookup mSpanSizeLookup = new SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return getItemViewType(position) == TYPE_MATCHING_HEADER ||
                        getItemViewType(position) == TYPE_ALL_HEADER ? 4 : 1;
            }
        };

        private GridAdapter(List<String> allDrawables, List<String> matchingDrawables) {
            mAllDrawables.add(null);
            mAllDrawables.addAll(allDrawables);
            mMatchingDrawables.add(null);
            mMatchingDrawables.addAll(matchingDrawables);
            mGridLayout.setSpanSizeLookup(mSpanSizeLookup);
            mNoMatchingDrawables = matchingDrawables.isEmpty();
            if (mNoMatchingDrawables) {
                mMatchingDrawables.clear();
            }
        }

        @Override
        public int getItemViewType(int position) {
            if (!mNoMatchingDrawables && position < mMatchingDrawables.size() &&
                    mMatchingDrawables.get(position) == null) {
                return TYPE_MATCHING_HEADER;
            }

            if (!mNoMatchingDrawables && position > TYPE_MATCHING_HEADER &&
                    position < mMatchingDrawables.size()) {
                return TYPE_MATCHING_ICONS;
            }

            if (position == mMatchingDrawables.size()) {
                return TYPE_ALL_HEADER;
            }
            return TYPE_ALL_ICONS;
        }

        @Override
        public int getItemCount() {
            return mAllDrawables.size() + 1;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            Activity activity = IconPickerActivity.this;
            if (viewType == TYPE_MATCHING_HEADER) {
                TextView text = (TextView) activity.getLayoutInflater().inflate(
                        R.layout.all_icons_view_header, null);
                text.setText(R.string.icon_pack_suggestions);
                return new ViewHolder(text);
            }
            if (viewType == TYPE_ALL_HEADER) {
                TextView text = (TextView) activity.getLayoutInflater().inflate(
                        R.layout.all_icons_view_header, null);
                text.setText(R.string.icon_pack_all_icons);
                return new ViewHolder(text);
            }

            ImageView view = new ImageView(activity);
            RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(
                    RecyclerView.LayoutParams.MATCH_PARENT, mIconSize);
            view.setLayoutParams(params);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            if (holder.getItemViewType() != TYPE_MATCHING_HEADER
                    && holder.getItemViewType() != TYPE_ALL_HEADER) {
                boolean drawablesMatching = holder.getItemViewType() == TYPE_MATCHING_ICONS;
                final List<String> drawables = drawablesMatching ?
                        mMatchingDrawables : mAllDrawables;
                if (holder.getAdapterPosition() >= drawables.size()) {
                    return;
                }
                holder.itemView.setOnClickListener(v -> {
                    Drawable icon = mIconsHandler.loadDrawable(mIconPackPackageName,
                            drawables.get(holder.getAdapterPosition()), true);
                    if (icon != null) {
                        mIconCache.addCustomInfoToDataBase(icon, sItemInfo, mCurrentPackageLabel);
                    }
                    IconPickerActivity.this.finish();
                });
                Drawable icon = null;
                String drawable = drawables.get(position);
                try {
                    icon = mIconsHandler.loadDrawable(mIconPackPackageName, drawable, true);
                } catch (OutOfMemoryError e) {
                    Log.e(TAG, e.getMessage());
                }
                if (icon != null) {
                    ((ImageView) holder.itemView).setImageDrawable(icon);
                }
            }
        }

        @Override
        public Filter getFilter() {
            return new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence charSequence) {
                    return new FilterResults();
                }

                @Override
                protected void publishResults(CharSequence charSequence, FilterResults results) {
                }
            };
        }


        void filterList(List<String> filteredDrawables, List<String> filteredMatches) {
            mAllDrawables = filteredDrawables;
            mMatchingDrawables = filteredMatches;
            notifyDataSetChanged();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            private ViewHolder(View v) {
                super(v);
            }
        }
    }

    private class GridItemSpacer extends RecyclerView.ItemDecoration {
        private int spacing;

        private GridItemSpacer(int spacing) {
            this.spacing = spacing;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                                   RecyclerView.State state) {
            outRect.top = spacing;
        }
    }
}