OSDN Git Service

02a247caa37f4c64686350e47abb91891a7e3c97
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / adapter / StartMenuAdapter.java
1 /* Copyright 2016 Braden Farmer
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 package com.farmerbb.taskbar.adapter;
17
18 import android.app.ActivityOptions;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.graphics.Rect;
24 import android.graphics.Typeface;
25 import android.hardware.display.DisplayManager;
26 import android.os.Build;
27 import android.support.v4.content.ContextCompat;
28 import android.support.v4.content.LocalBroadcastManager;
29 import android.view.Display;
30 import android.view.LayoutInflater;
31 import android.view.MotionEvent;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.ArrayAdapter;
35 import android.widget.ImageView;
36 import android.widget.LinearLayout;
37 import android.widget.TextView;
38
39 import com.farmerbb.taskbar.R;
40 import com.farmerbb.taskbar.activity.ContextMenuActivity;
41 import com.farmerbb.taskbar.activity.ContextMenuActivityDark;
42 import com.farmerbb.taskbar.util.AppEntry;
43 import com.farmerbb.taskbar.util.TopApps;
44 import com.farmerbb.taskbar.util.U;
45
46 import java.util.List;
47
48 import static android.content.Context.DISPLAY_SERVICE;
49
50 public class StartMenuAdapter extends ArrayAdapter<AppEntry> {
51
52     private boolean isGrid = false;
53
54     public StartMenuAdapter(Context context, int layout, List<AppEntry> list) {
55         super(context, layout, list);
56         isGrid = layout == R.layout.row_alt;
57     }
58
59     @Override
60     public View getView(int position, View convertView, final ViewGroup parent) {
61         // Check if an existing view is being reused, otherwise inflate the view
62         if(convertView == null)
63             convertView = LayoutInflater.from(getContext()).inflate(isGrid ? R.layout.row_alt : R.layout.row, parent, false);
64
65         final AppEntry entry = getItem(position);
66         final SharedPreferences pref = U.getSharedPreferences(getContext());
67
68         TextView textView = (TextView) convertView.findViewById(R.id.name);
69         textView.setText(entry.getLabel());
70
71         Intent intent = new Intent();
72         intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
73         String name = intent.resolveActivityInfo(getContext().getPackageManager(), 0).name;
74
75         TopApps topApps = TopApps.getInstance(getContext());
76         textView.setTypeface(null, topApps.isTopApp(name) ? Typeface.BOLD : Typeface.NORMAL);
77
78         switch(pref.getString("theme", "light")) {
79             case "light":
80                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color));
81                 break;
82             case "dark":
83                 textView.setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_dark));
84                 break;
85         }
86
87         ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
88         imageView.setImageDrawable(entry.getIcon(getContext()));
89
90         LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.entry);
91         layout.setOnClickListener(new View.OnClickListener() {
92             @Override
93             public void onClick(View view) {
94                 U.launchApp(getContext(), entry.getPackageName(), entry.getComponentName(), false);
95             }
96         });
97
98         layout.setOnLongClickListener(new View.OnLongClickListener() {
99             @Override
100             public boolean onLongClick(View view) {
101                 int[] location = new int[2];
102                 view.getLocationOnScreen(location);
103                 openContextMenu(entry, location);
104                 return true;
105             }
106         });
107
108         layout.setOnGenericMotionListener(new View.OnGenericMotionListener() {
109             @Override
110             public boolean onGenericMotion(View view, MotionEvent motionEvent) {
111                 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
112                         && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
113                     int[] location = new int[2];
114                     view.getLocationOnScreen(location);
115                     openContextMenu(entry, location);
116                 }
117
118                 return false;
119             }
120         });
121
122         return convertView;
123     }
124
125     @SuppressWarnings("deprecation")
126     private void openContextMenu(AppEntry entry, int[] location) {
127         LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent("com.farmerbb.taskbar.HIDE_START_MENU"));
128
129         SharedPreferences pref = U.getSharedPreferences(getContext());
130         Intent intent = null;
131
132         switch(pref.getString("theme", "light")) {
133             case "light":
134                 intent = new Intent(getContext(), ContextMenuActivity.class);
135                 break;
136             case "dark":
137                 intent = new Intent(getContext(), ContextMenuActivityDark.class);
138                 break;
139         }
140
141         if(intent != null) {
142             intent.putExtra("package_name", entry.getPackageName());
143             intent.putExtra("app_name", entry.getLabel());
144             intent.putExtra("component_name", entry.getComponentName());
145             intent.putExtra("launched_from_start_menu", true);
146             intent.putExtra("x", location[0]);
147             intent.putExtra("y", location[1]);
148             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
149         }
150
151         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && pref.getBoolean("freeform_hack", false)) {
152             DisplayManager dm = (DisplayManager) getContext().getSystemService(DISPLAY_SERVICE);
153             Display display = dm.getDisplay(Display.DEFAULT_DISPLAY);
154
155             getContext().startActivity(intent, ActivityOptions.makeBasic().setLaunchBounds(new Rect(0, 0, display.getWidth(), display.getHeight())).toBundle());
156         } else
157             getContext().startActivity(intent);
158     }
159 }