-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathAppsAdapterPreloadModel.java
More file actions
103 lines (88 loc) · 3.28 KB
/
AppsAdapterPreloadModel.java
File metadata and controls
103 lines (88 loc) · 3.28 KB
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
/*
* Copyright (C) 2014-2026 Arpit Khurana <arpitkh96@gmail.com>, Vishal Nehra <vishalmeham2@gmail.com>,
* Emmanuel Messulam<emmanuelbendavid@gmail.com>, Raymond Lai <airwave209gt at gmail.com> and Contributors.
*
* This file is part of Amaze File Manager.
*
* Amaze File Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.amaze.filemanager.adapters.glide;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amaze.filemanager.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.ListPreloader;
import com.bumptech.glide.RequestBuilder;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
/**
* @author Emmanuel Messulam <emmanuelbendavid@gmail.com> on 10/12/2017, at 15:38.
*/
public class AppsAdapterPreloadModel implements ListPreloader.PreloadModelProvider<String> {
private final Logger LOG = LoggerFactory.getLogger(AppsAdapterPreloadModel.class);
private Context mContext;
private RequestBuilder<Drawable> request;
private List<String> items;
private boolean isBottomSheet;
public AppsAdapterPreloadModel(Fragment f, boolean isBottomSheet) {
request = Glide.with(f).asDrawable().fitCenter();
this.mContext = f.requireContext();
this.isBottomSheet = isBottomSheet;
}
public void addItem(String item) {
if (items == null) {
items = new ArrayList<>();
}
items.add(item);
}
@NonNull
@Override
public List<String> getPreloadItems(int position) {
if (items == null) return Collections.emptyList();
else return Collections.singletonList(items.get(position));
}
@Nullable
@Override
public RequestBuilder getPreloadRequestBuilder(String item) {
if (isBottomSheet) {
return request.clone().load(getApplicationIconFromPackageName(item));
} else {
return request.clone().load(item);
}
}
public void loadApkImage(String item, AppCompatImageView v) {
if (isBottomSheet) {
request.load(getApplicationIconFromPackageName(item)).into(v);
} else {
request.load(item).into(v);
}
}
private Drawable getApplicationIconFromPackageName(String packageName) {
try {
return mContext.getPackageManager().getApplicationIcon(packageName);
} catch (PackageManager.NameNotFoundException e) {
LOG.warn(getClass().getSimpleName(), e);
return ContextCompat.getDrawable(mContext, R.drawable.ic_broken_image_white_24dp);
}
}
}