Java tutorial
/* * Copyright (c) 2015. NB Plus (www.nbplus.co.kr) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.nbplus.vbroadlauncher; import android.app.Activity; import android.app.ActivityManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; import android.provider.Browser; import android.support.v4.app.NotificationCompat; import android.support.v4.content.LocalBroadcastManager; import android.telephony.TelephonyManager; import android.util.Log; import android.widget.Toast; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.nbplus.iotlib.IoTInterface; import com.nbplus.iotlib.data.IoTDevice; import com.nbplus.push.data.PushConstants; import com.nbplus.push.data.PushMessageData; import com.nbplus.vbroadlauncher.data.Constants; import com.nbplus.vbroadlauncher.data.LauncherSettings; import com.nbplus.vbroadlauncher.data.PushPayloadData; import org.basdroid.common.PackageUtils; import org.basdroid.common.StringUtils; import java.util.List; /** * . */ public class BroadcastPushReceiver extends BroadcastReceiver { private static final String TAG = BroadcastPushReceiver.class.getName(); public BroadcastPushReceiver() { } @Override public void onReceive(Context context, Intent intent) { if (intent == null) { return; } Intent pi; String action = intent.getAction(); if (PushConstants.ACTION_PUSH_STATUS_CHANGED.equals(action)) { LocalBroadcastManager.getInstance(context).sendBroadcast(intent); } else if (PushConstants.ACTION_PUSH_MESSAGE_RECEIVED.equals(action)) { Log.d(TAG, "Receive.. broadcast ACTION_PUSH_MESSAGE_RECEIVED from push service. re-direct to activity!!!"); //TODO: iot ?. // int i = 0; // if (i == 0) { // return; // } PushMessageData data = null; try { data = (PushMessageData) intent.getParcelableExtra(PushConstants.EXTRA_PUSH_MESSAGE_DATA); if (data == null || StringUtils.isEmptyString(data.getPayload())) { Log.d(TAG, "empty push message string !!"); return; } } catch (Exception e) { e.printStackTrace(); return; } PushPayloadData payloadData = null; try { Gson gson = new GsonBuilder().create(); payloadData = gson.fromJson(data.getPayload(), new TypeToken<PushPayloadData>() { }.getType()); } catch (Exception e) { e.printStackTrace(); } if (payloadData == null) { Log.d(TAG, "empty push message data !!"); return; } String type = payloadData.getServiceType(); Log.d(TAG, "HANDLER_MESSAGE_PUSH_MESAGE_RECEIVED received type = " + type + ", messageId = " + payloadData.getMessageId()); payloadData.setAlertMessage(data.getAlert()); switch (type) { // case Constants.PUSH_PAYLOAD_TYPE_REALTIME_BROADCAST: case Constants.PUSH_PAYLOAD_TYPE_NORMAL_BROADCAST: case Constants.PUSH_PAYLOAD_TYPE_TEXT_BROADCAST: TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); int callState = telephonyManager.getCallState(); boolean isOutdoor = LauncherSettings.getInstance(context).isOutdoorMode(); playNotificationAlarm(context, R.string.notification_broadcast_push); if (isOutdoor || callState == TelephonyManager.CALL_STATE_OFFHOOK) { // ? ? ?. Log.d(TAG, "Broadcast notification.. isOutdoor mode... "); pi = new Intent(); pi.setAction(action); pi.putExtra(PushConstants.EXTRA_PUSH_STATUS_VALUE, intent.getIntExtra( PushConstants.EXTRA_PUSH_STATUS_VALUE, PushConstants.PUSH_STATUS_VALUE_DISCONNECTED)); pi.putExtra(Constants.EXTRA_BROADCAST_PAYLOAD_DATA, payloadData); LocalBroadcastManager.getInstance(context).sendBroadcast(pi); return; } if (Constants.OPEN_BETA_PHONE) { String activePackageName = PackageUtils.getActivePackage(context); if (activePackageName != null && !StringUtils.isEmptyString(activePackageName)) { if (Constants.VBROAD_SEND_APP_PACKAGE.equals(activePackageName)) { Log.d(TAG, Constants.VBROAD_SEND_APP_PACKAGE + " is top running application...."); pi = new Intent(); pi.setAction(action); pi.putExtra(PushConstants.EXTRA_PUSH_STATUS_VALUE, intent.getIntExtra(PushConstants.EXTRA_PUSH_STATUS_VALUE, PushConstants.PUSH_STATUS_VALUE_DISCONNECTED)); pi.putExtra(Constants.EXTRA_BROADCAST_PAYLOAD_DATA, payloadData); LocalBroadcastManager.getInstance(context).sendBroadcast(pi); return; } } } // ?? ?. ? Domain url ?? . if (PackageUtils.isActivePackage(context, Constants.GOOGLE_CHROME_PACKAGE_NAME)) { Log.d(TAG, "chrome browser is active...."); // get the last visited URL from the Browser.BOOKMARKS_URI database String url = ""; Uri chromeUri = Uri.parse("content://com.android.chrome.browser/bookmarks"); Cursor cur = context.getContentResolver().query(chromeUri/*Browser.BOOKMARKS_URI*/, new String[] { Browser.BookmarkColumns.URL }, null, null, Browser.BookmarkColumns.DATE + " DESC"); if (cur != null && cur.getCount() > 0) { cur.moveToFirst(); url = cur.getString(cur.getColumnIndex(Browser.BookmarkColumns.URL)); cur.close(); } else { if (cur != null) { cur.close(); } } Log.d(TAG, "Last activated url = " + url); if (!StringUtils.isEmptyString(url) && (url.startsWith(Constants.VBROAD_HTTP_DOMAIN) || url.startsWith(Constants.VBROAD_HTTPS_DOMAIN))) { Log.w(TAG, ">> Village broadcast is running... do not show!!!!"); pi = new Intent(); pi.setAction(action); pi.putExtra(PushConstants.EXTRA_PUSH_STATUS_VALUE, intent.getIntExtra(PushConstants.EXTRA_PUSH_STATUS_VALUE, PushConstants.PUSH_STATUS_VALUE_DISCONNECTED)); pi.putExtra(Constants.EXTRA_BROADCAST_PAYLOAD_DATA, payloadData); LocalBroadcastManager.getInstance(context).sendBroadcast(pi); return; } } // android version check. if (Constants.PUSH_PAYLOAD_TYPE_REALTIME_BROADCAST.equals(type) || Constants.PUSH_PAYLOAD_TYPE_NORMAL_BROADCAST.equals(type)) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Log.d(TAG, ">> This device version code = " + Build.VERSION.SDK_INT + ", not supported version !!"); Toast.makeText(context, R.string.notification_broadcast_not_support, Toast.LENGTH_SHORT); pi = new Intent(); pi.setAction(action); pi.putExtra(PushConstants.EXTRA_PUSH_STATUS_VALUE, intent.getIntExtra(PushConstants.EXTRA_PUSH_STATUS_VALUE, PushConstants.PUSH_STATUS_VALUE_DISCONNECTED)); pi.putExtra(Constants.EXTRA_BROADCAST_PAYLOAD_DATA, payloadData); LocalBroadcastManager.getInstance(context).sendBroadcast(pi); break; } } /*boolean useServiceChatHead = false; if (useServiceChatHead) { i = new Intent(context, RealtimeBroadcastProxyActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.setAction(action); i.putExtra(Constants.EXTRA_BROADCAST_PAYLOAD_DATA, payloadData); context.startActivity(i); } else*/ String playingType = LauncherSettings.getInstance(context).getCurrentPlayingBroadcastType(); if (StringUtils.isEmptyString(playingType) || !Constants.PUSH_PAYLOAD_TYPE_REALTIME_BROADCAST.equals(playingType)) { // ??? ... ? . ??? pi = new Intent(context, RealtimeBroadcastActivity.class); pi.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); pi.setAction(action); pi.putExtra(Constants.EXTRA_BROADCAST_PAYLOAD_DATA, payloadData); pi.putExtra(Constants.EXTRA_BROADCAST_PAYLOAD_INDEX, System.currentTimeMillis()); Log.d(TAG, "1. sendBroadcast() >> ACTION_PUSH_MESSAGE_RECEIVED : idx = " + pi.getLongExtra(Constants.EXTRA_BROADCAST_PAYLOAD_INDEX, -1)); // ? ms ? ?? ? ? ? ???? // broadcast ? . // ?? ?? ??. //LocalBroadcastManager.getInstance(context).sendBroadcast(pi); try { //Thread.sleep(30); context.startActivity(pi); } catch (Exception e) { e.printStackTrace(); } } else { // ?? ? ??... ? ?.. ? . if (Constants.PUSH_PAYLOAD_TYPE_REALTIME_BROADCAST.equals(playingType) && Constants.PUSH_PAYLOAD_TYPE_REALTIME_BROADCAST.equals(type)) { pi = new Intent(context, RealtimeBroadcastActivity.class); pi.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); pi.setAction(action); pi.putExtra(Constants.EXTRA_BROADCAST_PAYLOAD_DATA, payloadData); pi.putExtra(Constants.EXTRA_BROADCAST_PAYLOAD_INDEX, System.currentTimeMillis()); Log.d(TAG, "2. sendBroadcast() >> ACTION_PUSH_MESSAGE_RECEIVED : idx = " + pi.getLongExtra(Constants.EXTRA_BROADCAST_PAYLOAD_INDEX, -1)); // ? ms ? ?? ? ? ? ???? // broadcast ? . // ?? ?? ??. //LocalBroadcastManager.getInstance(context).sendBroadcast(pi); try { //Thread.sleep(30); context.startActivity(pi); } catch (Exception e) { e.printStackTrace(); } } else { // ? ? ? ??. Log.d(TAG, "? ? ? ??."); Toast.makeText(context, payloadData.getAlertMessage(), Toast.LENGTH_SHORT).show(); } } break; // case Constants.PUSH_PAYLOAD_TYPE_EMERGENCY_CALL: break; // case Constants.PUSH_PAYLOAD_TYPE_INHABITANTS_POLL: // ? case Constants.PUSH_PAYLOAD_TYPE_COOPERATIVE_BUYING: int strId = Constants.PUSH_PAYLOAD_TYPE_INHABITANTS_POLL.equals(payloadData.getServiceType()) ? R.string.notification_inhabitant_push : R.string.notification_cooperative_buying_push; playNotificationAlarm(context, strId); pi = new Intent(); pi.setAction(action); pi.putExtra(PushConstants.EXTRA_PUSH_STATUS_VALUE, intent.getIntExtra( PushConstants.EXTRA_PUSH_STATUS_VALUE, PushConstants.PUSH_STATUS_VALUE_DISCONNECTED)); pi.putExtra(Constants.EXTRA_BROADCAST_PAYLOAD_DATA, payloadData); Log.d(TAG, "3. sendBroadcast() >> ACTION_PUSH_MESSAGE_RECEIVED : idx = " + 0); LocalBroadcastManager.getInstance(context).sendBroadcast(pi); break; // IOT DEVICE ( ) case Constants.PUSH_PAYLOAD_TYPE_IOT_DEVICE_CONTROL: Log.d(TAG, "startService >> ACTION_SEND_IOT_COMMAND"); IoTInterface.getInstance().controlDevice(payloadData.getIotControlDeviceId(), payloadData.getMessage()); break; // PUSH_PAYLOAD_TYPE_PUSH_NOTIFICATION case Constants.PUSH_PAYLOAD_TYPE_PUSH_NOTIFICATION: // ?... // pi = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(payloadData.getMessage())); // showNotification(context, Constants.SYSTEM_ADMIN_NOTIFICATION_ID, PackageUtils.getApplicationName(context), payloadData.getAlertMessage(), null, pi); // bigText showNotification(context, Constants.SYSTEM_ADMIN_NOTIFICATION_ID, PackageUtils.getApplicationName(context), payloadData.getAlertMessage(), PackageUtils.getApplicationName(context), payloadData.getMessage(), null, null, null); break; // IOT DEVICE ( ) case Constants.PUSH_PAYLOAD_TYPE_FIND_PASSWORD: pi = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(payloadData.getMessage())); showNotification(context, Constants.PW_FIND_NOTIFICATION_ID, PackageUtils.getApplicationName(context), payloadData.getAlertMessage(), null, pi); break; default: Log.d(TAG, "Unknown push payload type !!!"); break; } } } private void playNotificationAlarm(Context context, int textResId) { NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSound(soundUri); notificationManager.notify(Constants.PUSH_NOTIFICATION_ALARM_ID, builder.build()); Toast.makeText(context, textResId, Toast.LENGTH_SHORT).show(); } private void showNotification(Context context, int notificationId, String title, String contentText, String ticker, Intent intent) { showNotification(context, notificationId, title, contentText, null, null, null, ticker, intent); } private void showNotification(Context context, int notificationId, String title, String contentText, String bigTitle, String bigContentText, String summaryText, String ticker, Intent intent) { NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSound(soundUri); builder.setSmallIcon(R.drawable.ic_notification_noti); builder.setWhen(System.currentTimeMillis()); //builder.setNumber(10); if (!StringUtils.isEmptyString(ticker)) { builder.setTicker(ticker); } if (StringUtils.isEmptyString(title)) { builder.setContentTitle(PackageUtils.getApplicationName(context)); } else { builder.setContentTitle(title); } builder.setContentText(contentText); builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); builder.setAutoCancel(true); // big title and text if (!StringUtils.isEmptyString(bigTitle) && !StringUtils.isEmptyString(bigContentText)) { NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle(builder); if (!StringUtils.isEmptyString(summaryText)) { style.setSummaryText(summaryText); } style.setBigContentTitle(bigTitle); style.bigText(bigContentText); builder.setStyle(style); } if (intent != null) { PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); } notificationManager.notify(notificationId, builder.build()); } }