Upgrade to Pro — share decks privately, control downloads, hide ads and more …

(続) Debug Menuはじめました。

operandoOS
October 27, 2017

(続) Debug Menuはじめました。

(続) Debug Menuはじめました。

shibuya.apk #19
https://shibuya-apk.connpass.com/event/68094/

Sample Project
https://github.com/operando/android-debug-menu-sample

operandoOS

October 27, 2017
Tweet

More Decks by operandoOS

Other Decks in Technology

Transcript

  1. private static final String NOTIFICATION_TAG = "DebugMenuNotificationManager"; private static final

    int NOTIFICATION_ID = Integer.MAX_VALUE; private static final String CHANNEL_ID = "debug"; public static void showDebugMenuNotification(Context c) { Intent i = new Intent(c, DebugMenuActivity.class); PendingIntent pi = PendingIntent.getActivity(c, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); Notification n = buildNotification(c, pi); NotificationManager nm = getNotificationManager(c); nm.notify(NOTIFICATION_TAG, NOTIFICATION_ID, n); } private static Notification buildNotification(Context c, PendingIntent pi) { PackageManager pm = c.getPackageManager(); String applicationName = c.getApplicationInfo().loadLabel(pm).toString(); NotificationCompat.Builder builder = new NotificationCompat.Builder(c, CHANNEL_ID); builder.setTicker("debug menu"); builder.setOngoing(true); builder.setContentTitle(applicationName); builder.setContentText("λοϓ͢ΔͱDebug Menu͕։͖·͢"); builder.setSmallIcon(R.drawable.ic_notification_small); builder.setContentIntent(pi); builder.setAutoCancel(false); return builder.build(); } private static NotificationManager getNotificationManager(Context c) { return (NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE); }
  2. private static final String CHANNEL_ID = "debug"; private static Notification

    buildNotification(Context c, PendingIntent pi) { PackageManager pm = c.getPackageManager(); String applicationName = c.getApplicationInfo().loadLabel(pm).toString(); NotificationCompat.Builder builder = new NotificationCompat.Builder(c, CHANNEL_ID); builder.setTicker("debug menu"); builder.setOngoing(true); builder.setContentTitle(applicationName); builder.setContentText("λοϓ͢ΔͱDebug Menu͕։͖·͢"); builder.setSmallIcon(R.drawable.ic_notification_small); builder.setContentIntent(pi); builder.setAutoCancel(false); return builder.build(); }
  3. private static final String NOTIFICATION_TAG = "DebugMenuNotificationManager"; private static final

    int NOTIFICATION_ID = Integer.MAX_VALUE; public static void showDebugMenuNotification(Context c) { Intent i = new Intent(c, DebugMenuActivity.class); PendingIntent pi = PendingIntent.getActivity(c, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); Notification n = buildNotification(c, pi); NotificationManager nm = getNotificationManager(c); nm.notify(NOTIFICATION_TAG, NOTIFICATION_ID, n); } private static NotificationManager getNotificationManager(Context c) { return (NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE); }
  4. ৗઃ௨஌͔ΒDebug Menuͷը໘Λ։͘ • ϝϧΧϦ Χ΢ϧͰ͸௨஌ͷදࣔɾΫϦΞͷ੍ޚʹ Application.ActivityLifecycleCallbacksΛ࢖ͬͯΔ • ΞϓϦ͕ىಈͨ࣌͠ʹ௨஌Λදࣔ͢Δ • Activityͷback

    stack্Ͱ࠷ޙͷActivity͕Destroy͢Δ࣌ʹ ௨஌ΛΫϦΞ͢Δ • ࣮૷ͨ͠Application.ActivityLifecycleCallbacks͸ ApplicationͷonCreateͱ͔Ͱొ࿥͢Δ
  5. public class DebugMenuActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks { private boolean isShowDebugMenuNotification; @Override

    public void onActivityCreated(Activity activity, Bundle bundle) { if (!isShowDebugMenuNotification) { DebugMenuNotificationManager.showDebugMenuNotification(activity); isShowDebugMenuNotification = true; } } @Override public void onActivityDestroyed(Activity activity) { if (activity instanceof MainActivity) { DebugMenuNotificationManager.cancelDebugMenuNotification(activity); isShowDebugMenuNotification = false; } } }
  6. public class DebugMenuActivity extends AppCompatActivity { @Override protected void onCreate(Bundle

    savedInstanceState) { super.onCreate(savedInstanceState); ActivityDebugMenuBinding binding = DataBindingUtil.setContentView(this, R.layout…); binding.openApplicationSettings.setOnClickListener(v -> { Intent i = new Intent(); i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); i.setData(Uri.fromParts("package", getPackageName(), null)); startActivity(i); }); } }
  7. public class UrlManager { public static final String API_URL =

    "https://tech-book-fest-debug.com"; private static DebugInformationPrefs debugInformationPrefs; public static void setDebugInformationPrefs(DebugInformationPrefs prefs) { UrlManager.debugInformationPrefs = prefs; } public static String getApiUrl() { // APIͷURLʹมߋ͕ͳ͍৔߹ɺఆٛࡁΈͷAPIͷURLΛฦ͢ if (debugInformationPrefs == null || !debugInformationPrefs.hasApiUrl()) { return API_URL; } return debugInformationPrefs.getApiUrl(); } }
  8. public class DebugMenuActivity extends AppCompatActivity { @Override protected void onCreate(Bundle

    savedInstanceState) { super.onCreate(savedInstanceState); ActivityDebugMenuBinding binding = DataBindingUtil.setContentView(this, R.layout…); binding.apiUrl.setText(UrlManager.getApiUrl()); binding.apiUrl.setSelection(binding.apiUrl.getText().toString().length()); binding.apiUrl.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) { DebugInformationPrefs.get(DebugMenuActivity.this) .setApiUrl(s.toString()); } }); } }
  9. public class DebugMenuActivity extends AppCompatActivity { @Override protected void onCreate(Bundle

    savedInstanceState) { super.onCreate(savedInstanceState); ActivityDebugMenuBinding binding = DataBindingUtil.setContentView(this, R.layout…); binding.picassoEnableDebugLog.setChecked( Picasso.with(this).isLoggingEnabled()); binding.picassoEnableDebugLog.setOnCheckedChangeListener((__, ic) -> { DebugInformationPrefs.get(this).setPicassoLoggingEnabled(ic); Picasso.with(this).setLoggingEnabled(ic); }); binding.picassoEnabledIndicators.setChecked( Picasso.with(this).areIndicatorsEnabled()); binding.picassoEnabledIndicators.setOnCheckedChangeListener((__, ic) -> { DebugInformationPrefs.get(this).setPicassoAreIndicatorsEnabled(ic); Picasso.with(this).setIndicatorsEnabled(ic); }); } }
  10. public class DebugApplication extends Application { @Override public void onCreate()

    { super.onCreate(); DebugInformationPrefs prefs = DebugInformationPrefs.get(this); Picasso picasso = Picasso.with(this); picasso.setLoggingEnabled(prefs.getPicassoLoggingEnabled()); picasso.setIndicatorsEnabled(prefs.getPicassoAreIndicatorsEnabled()); } }
  11. <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...> <Button style="@style/Button.A" android:text="Button A" />

    <Button style="@style/Button.B" android:text="Button B" /> <Button style="@style/Button.C" android:text="Button C" /> </LinearLayout>