1 package com.android.launcher3.util;
2 
3 import android.content.ComponentName;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.SharedPreferences;
7 import android.util.Log;
8 import android.view.Gravity;
9 import android.view.View;
10 import android.widget.FrameLayout;
11 
12 import com.android.launcher3.CustomAppWidget;
13 import com.android.launcher3.Launcher;
14 import com.android.launcher3.LauncherAppState;
15 import com.android.launcher3.R;
16 import com.android.launcher3.Utilities;
17 
18 import java.util.HashMap;
19 
20 public class TestingUtils {
21 
22     public static final String MEMORY_TRACKER = "com.android.launcher3.testing.MemoryTracker";
23     public static final String ACTION_START_TRACKING = "com.android.launcher3.action.START_TRACKING";
24 
25     public static final boolean MEMORY_DUMP_ENABLED = false;
26     public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
27 
28     public static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
29     public static final String DUMMY_WIDGET = "com.android.launcher3.testing.DummyWidget";
30 
startTrackingMemory(Context context)31     public static void startTrackingMemory(Context context) {
32         if (MEMORY_DUMP_ENABLED) {
33             context.startService(new Intent()
34                 .setComponent(new ComponentName(context.getPackageName(), MEMORY_TRACKER))
35                 .setAction(ACTION_START_TRACKING)
36                 .putExtra("pid", android.os.Process.myPid())
37                 .putExtra("name", "L"));
38         }
39     }
40 
addWeightWatcher(Launcher launcher)41     public static void addWeightWatcher(Launcher launcher) {
42         if (MEMORY_DUMP_ENABLED) {
43             boolean show = Utilities.getPrefs(launcher).getBoolean(SHOW_WEIGHT_WATCHER, true);
44 
45             int id = launcher.getResources().getIdentifier("zzz_weight_watcher", "layout",
46                     launcher.getPackageName());
47             View watcher = launcher.getLayoutInflater().inflate(id, null);
48             watcher.setAlpha(0.5f);
49             ((FrameLayout) launcher.findViewById(R.id.launcher)).addView(watcher,
50                     new FrameLayout.LayoutParams(
51                             FrameLayout.LayoutParams.MATCH_PARENT,
52                             FrameLayout.LayoutParams.WRAP_CONTENT,
53                             Gravity.BOTTOM)
54             );
55 
56             watcher.setVisibility(show ? View.VISIBLE : View.GONE);
57             launcher.mWeightWatcher = watcher;
58         }
59     }
60 
addDummyWidget(HashMap<String, CustomAppWidget> set)61     public static void addDummyWidget(HashMap<String, CustomAppWidget> set) {
62         if (ENABLE_CUSTOM_WIDGET_TEST) {
63             try {
64                 Class<?> clazz = Class.forName(DUMMY_WIDGET);
65                 CustomAppWidget widget = (CustomAppWidget) clazz.newInstance();
66                 set.put(widget.getClass().getName(), widget);
67             } catch (Exception e) {
68                 Log.e("TestingUtils", "Error adding dummy widget", e);
69             }
70         }
71     }
72 }
73