|
| 1 | +// ©2020 Datalogic S.p.A. and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +package com.datalogic.examples.seconddisplaysampleapi; |
| 4 | + |
| 5 | +import android.app.Activity; |
| 6 | +import android.app.NotificationChannel; |
| 7 | +import android.app.NotificationManager; |
| 8 | +import android.os.Bundle; |
| 9 | +import android.support.v4.app.NotificationCompat; |
| 10 | +import android.util.Log; |
| 11 | +import android.view.Menu; |
| 12 | +import android.view.MotionEvent; |
| 13 | +import android.view.View; |
| 14 | +import android.view.Window; |
| 15 | +import android.view.WindowManager; |
| 16 | +import android.view.View.OnTouchListener; |
| 17 | +import android.widget.Button; |
| 18 | +import android.app.Notification; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import com.datalogic.device.display.DLSecondDisplayManager; |
| 22 | + |
| 23 | +public class MainActivity extends Activity { |
| 24 | + |
| 25 | + static final String TAG = "Test-Scanner"; |
| 26 | + private final String PACKAGE_NAME = "com.datalogic.examples.seconddisplaysampleapi"; |
| 27 | + |
| 28 | + // The send notification button |
| 29 | + private Button mSend; |
| 30 | + |
| 31 | + private final String NOTIFICATION_CHANNEL_ID = "2nd Display Channel"; |
| 32 | + private final String NOTIFICATION_CHANNEL_NAME = "2nd display notification sample channel"; |
| 33 | + |
| 34 | + private final int NOTIFICATION_ID = 0; |
| 35 | + private final String NOTIFICATION_TITLE_TEXT = "2nd Display Sample"; |
| 36 | + private final String NOTIFICATION_CONTENT_TEXT = "this sample notification is rolling on second display!"; |
| 37 | + |
| 38 | + private NotificationManager mNotificationManager = null; |
| 39 | + private Notification mNotification = null; |
| 40 | + |
| 41 | + @Override |
| 42 | + protected void onCreate(Bundle savedInstanceState) { |
| 43 | + super.onCreate(savedInstanceState); |
| 44 | + Window window = getWindow(); |
| 45 | + window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 46 | + setContentView(R.layout.activity_main); |
| 47 | + |
| 48 | + // 2nd Display configuration to enable the App |
| 49 | + // to shown notification on 2nd Display |
| 50 | + setupSecondDisplay(); |
| 51 | + |
| 52 | + // Configure the Notification System to |
| 53 | + // allow the App to send notifications |
| 54 | + setupNotificationSystem(); |
| 55 | + |
| 56 | + // create the View with the button |
| 57 | + // to send notifications |
| 58 | + setupView(); |
| 59 | + } |
| 60 | + |
| 61 | + private void setupNotificationSystem() { |
| 62 | + mNotificationManager = getSystemService(NotificationManager.class); |
| 63 | + CharSequence channelName = NOTIFICATION_CHANNEL_NAME; |
| 64 | + |
| 65 | + NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); |
| 66 | + mNotificationManager.createNotificationChannel(notificationChannel); |
| 67 | + |
| 68 | + NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID) |
| 69 | + .setSmallIcon(R.drawable.ic_launcher) |
| 70 | + .setContentTitle(NOTIFICATION_TITLE_TEXT) |
| 71 | + .setContentText(NOTIFICATION_CONTENT_TEXT) |
| 72 | + .setPriority(NotificationCompat.PRIORITY_HIGH); |
| 73 | + |
| 74 | + mNotification = builder.build(); |
| 75 | + } |
| 76 | + |
| 77 | + private void setupSecondDisplay() { |
| 78 | + // Retrieve the reference to the 2nd Display Manager |
| 79 | + DLSecondDisplayManager secondDisplayManager = DLSecondDisplayManager.getInstance(); |
| 80 | + |
| 81 | + // Check if the 2nd Display is available on the current device |
| 82 | + // 2nd Display is only available for Datalogic Memor20 |
| 83 | + boolean available = secondDisplayManager.isSecondDisplayAvailable(); |
| 84 | + |
| 85 | + // if available, 2nd Display can be configured |
| 86 | + if (available) { |
| 87 | + // if disable, enable 2nd Display to show notifications |
| 88 | + boolean enabled = secondDisplayManager.isSecondDisplayEnabled(); |
| 89 | + if (!enabled) { |
| 90 | + boolean result = secondDisplayManager.setSecondDisplayEnabled(true); |
| 91 | + if (!result) |
| 92 | + Log.d(TAG, "Error occurred enabling 2nd Display"); |
| 93 | + } |
| 94 | + |
| 95 | + // disable all configured Apps to show notification on 2nd Display |
| 96 | + List<String> allowedPackage = secondDisplayManager.getAllowedPackages(); |
| 97 | + for (String packageName : allowedPackage) { |
| 98 | + boolean result = secondDisplayManager.removePackage(packageName); |
| 99 | + if (!result) |
| 100 | + Log.d(TAG, "Error disabling " + packageName + " to show notifications on 2nd Display"); |
| 101 | + } |
| 102 | + |
| 103 | + // disable by default Apps installed in the future |
| 104 | + boolean newAppEnabledByDefault = secondDisplayManager.getNewAppEnabled(); |
| 105 | + if (newAppEnabledByDefault) { |
| 106 | + // This feature allow to configure the 2nd Display to discard by default |
| 107 | + // the notifications coming from Apps will be installed later. |
| 108 | + // With the following configuration, when a new App is installed on the device, |
| 109 | + // it shall be explicitly configured through "addPackage" to show notifications |
| 110 | + // on 2nd Display. |
| 111 | + // These operation could take a while, due to the storage of all the |
| 112 | + // installed Apps into the DB. |
| 113 | + secondDisplayManager.setNewAppEnabled(false); |
| 114 | + } |
| 115 | + |
| 116 | + // enable only this App to show notification on 2nd Display |
| 117 | + boolean allowed = secondDisplayManager.isPackageAllowed(PACKAGE_NAME); |
| 118 | + if (!allowed) { |
| 119 | + boolean added = secondDisplayManager.addPackage(PACKAGE_NAME); |
| 120 | + if (!added) |
| 121 | + Log.d(TAG, "Error occurred allowing " + PACKAGE_NAME + " to show notification on 2nd Display"); |
| 122 | + } |
| 123 | + } else { |
| 124 | + Log.d(TAG, "2nd Display is available only on Datlalogic Memor20"); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void setupView() { |
| 129 | + mSend = (Button) findViewById(R.id.send); |
| 130 | + mSend.setOnTouchListener(new OnTouchListener() { |
| 131 | + |
| 132 | + @Override |
| 133 | + public boolean onTouch(View v, MotionEvent event) { |
| 134 | + if (event.getAction() == MotionEvent.ACTION_UP) { |
| 135 | + try { |
| 136 | + mNotificationManager.notify(NOTIFICATION_ID, mNotification); |
| 137 | + mSend.setPressed(false); |
| 138 | + } catch (Exception e) { |
| 139 | + Log.e(TAG, "Action UP", e); |
| 140 | + //showMessage("ERROR! Check logcat"); |
| 141 | + } |
| 142 | + v.performClick(); |
| 143 | + } |
| 144 | + return true; |
| 145 | + } |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + protected void onPause() { |
| 151 | + super.onPause(); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + protected void onResume() { |
| 156 | + super.onResume(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 161 | + getMenuInflater().inflate(R.menu.main, menu); |
| 162 | + return true; |
| 163 | + } |
| 164 | +} |
0 commit comments