</activity>
<activity
android:name=".NotificationActivity"
+ android:theme="@style/ICNotification"
android:launchMode="singleTop">
package com.ic.launcher;
import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.ImageView;
+import android.widget.LinearLayout;
+import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
+import com.ic.iclooger.ICLogger;
+import com.ic.launcher.data.NotificationItem;
import com.ic.launcher.presenter.NotificationPresenter;
+import java.text.SimpleDateFormat;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
public class NotificationActivity extends AppCompatActivity {
private NotificationPresenter notificationPresenter;
+ private LinearLayout container;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
+ initView();
notificationPresenter = NotificationPresenter.getsInstance(this);
+ initObserver();
+ }
+
+ private void initView() {
+ container = findViewById(R.id.notification_container);
+ }
+
+ private void initObserver() {
+ if (notificationPresenter != null) {
+ //更新到列表里
+ notificationPresenter.notificationListValue.observe(this, this::renderList);
+ }
+ }
+
+ //如果同category的超过4个,就合并为1个
+ private final Map<String, Integer> itemCategoryCount = new HashMap<>();
+
+ /**
+ * 渲染UI列表
+ *
+ * @param list
+ */
+ private void renderList(List<NotificationItem> list) {
+ container.removeAllViews();
+ itemCategoryCount.clear();
+ //根据类型去加载数据
+ if (list != null) {
+ //如果同category的超过4个,就合并为1个
+ //一共有健康警告,短信,未接电话三种类型
+ for (int i = 0; i < list.size(); i++) {
+ NotificationItem notificationItem = list.get(i);
+ handleItemInsert(notificationItem);
+ }
+ }
+ }
+
+ private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd hh:mm");
+
+ //创建Item,插入,并且处理满4进1的做法
+ private void handleItemInsert(NotificationItem notificationItem) {
+ String category = notificationItem.getCategory();
+ Integer integer = itemCategoryCount.get(category);
+ ICLogger.d("item count == > " + integer);
+ View itemView = LayoutInflater.from(this).inflate(
+ R.layout.item_notification, container, false
+ );
+ //赋值
+ TextView titleTv = itemView.findViewById(R.id.notification_title);
+ TextView timeTv = itemView.findViewById(R.id.notification_time);
+ ImageView leftIconIv = itemView.findViewById(R.id.left_icon);
+ ImageView rightDot = itemView.findViewById(R.id.notification_dot);
+ rightDot.setImageResource(R.mipmap.icon_notification_dot_normal);
+ //设置数据
+ titleTv.setText(notificationItem.getTitle());
+ long date = notificationItem.getDate();
+ String format = simpleDateFormat.format(date);
+ timeTv.setText(format);
+ itemView.setTag(category);
+ //处理头像
+ int iconRes = getIconRes(notificationItem.getType());
+ leftIconIv.setImageResource(iconRes);
+ if (integer == null) {
+ itemCategoryCount.put(category, 1);
+ container.addView(itemView);
+ } else if (integer < 3) {
+ integer++;
+ itemCategoryCount.put(category, integer);
+ container.addView(itemView);
+ } else {
+ integer++;
+ itemCategoryCount.put(category, integer);
+ //慢四,合并为1个View
+ int childCount = container.getChildCount();
+ for (int i = 0; i < childCount; i++) {
+ View childAt = container.getChildAt(i);
+ if (childAt != null && category.equals(childAt.getTag())) {
+ childAt.setVisibility(View.GONE);
+ }
+ }
+ //设置红点
+ rightDot.setImageResource(R.mipmap.icon_notification_dot_active);
+ container.addView(itemView);
+ }
+ }
+
+ public static final String TYPE_SMS = "sms";
+ public static final String TYPE_HEART_RATE = "heart_rate";
+ public static final String TYPE_BLOOD_PRESSURE = "blood_pressure";
+ public static final String TYPE_BLOOD_OXYGEN = "blood_oxygen";
+ public static final String TYPE_MISSED_PHONE = "missed_phone";
+ public static final String TYPE_BODY_TEMPERATURE = "body_temperature";
+
+
+ private int getIconRes(String type) {
+ switch (type) {
+ case TYPE_SMS:
+ return R.mipmap.icon_notification_sms;
+ case TYPE_HEART_RATE:
+ return R.mipmap.icon_notification_heart_rate;
+ case TYPE_BLOOD_PRESSURE:
+ return R.mipmap.icon_notification_blood_pressure;
+ case TYPE_BLOOD_OXYGEN:
+ return R.mipmap.icon_notification_blood_oxygen;
+ case TYPE_MISSED_PHONE:
+ return R.mipmap.icon_notification_missed_phone;
+ case TYPE_BODY_TEMPERATURE:
+ return R.mipmap.icon_notification_tempearture;
+ }
+ return -1;
}
@Override
if (viewType == TYPE_MAIN) {
//主页面
itemView = new MainPageView(parent.getContext());
- ((MainPageView) itemView).initViewModel(mLifeOwner);
+ ((MainPageView) itemView).initViewModel();
ViewPager2.LayoutParams layoutParams = new ViewPager2.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
import androidx.lifecycle.MutableLiveData;
import com.ic.iclooger.ICLogger;
+import com.ic.launcher.NotificationActivity;
import com.ic.launcher.R;
import org.threeten.bp.Instant;
this.mContext = context;
//监听数据变化
executor = Executors.newSingleThreadExecutor();
- executor.execute(() -> {
- //监听网路广播
- registerTimeDateReceiver();
- updateTime(null);
- updateTemperature();
- updateOxygen();
- updateEnvTemp();
- updateHeartRate();
- observerContent(context);
- });
+ //监听网路广播
+ registerTimeDateReceiver();
+ updateTemperature();
+ updateOxygen();
+ updateEnvTemp();
+ updateHeartRate();
+ observerContent(context);
+ checkNotification();
+ }
+
+ private void checkNotification() {
+ Cursor query = mContext.getContentResolver().query(Uri.parse(NOTIFICATIONS),
+ null, null, null, null);
+ if (query != null) {
+ if (query.moveToNext()) {
+ mContext.startActivity(new Intent(mContext, NotificationActivity.class));
+ }
+ query.close();
+ }
}
private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
private void computeMainClockTime(LocalDateTime localDateTime) {
switch (localDateTime.getDayOfWeek()) {
case MONDAY:
- weekValue.setValue(mContext.getString(R.string.monday));
+ weekValue.postValue(mContext.getString(R.string.monday));
break;
case TUESDAY:
- weekValue.setValue(mContext.getString(R.string.tuesday));
+ weekValue.postValue(mContext.getString(R.string.tuesday));
break;
case WEDNESDAY:
- weekValue.setValue(mContext.getString(R.string.wednesday));
+ weekValue.postValue(mContext.getString(R.string.wednesday));
break;
case THURSDAY:
- weekValue.setValue(mContext.getString(R.string.thursday));
+ weekValue.postValue(mContext.getString(R.string.thursday));
break;
case FRIDAY:
- weekValue.setValue(mContext.getString(R.string.friday));
+ weekValue.postValue(mContext.getString(R.string.friday));
break;
case SATURDAY:
- weekValue.setValue(mContext.getString(R.string.saturday));
+ weekValue.postValue(mContext.getString(R.string.saturday));
break;
case SUNDAY:
- weekValue.setValue(mContext.getString(R.string.sunday));
+ weekValue.postValue(mContext.getString(R.string.sunday));
break;
}
}
@Override
public void onChange(boolean selfChange) {
//打开通知列表
-
+ mContext.startActivity(new Intent(mContext, NotificationActivity.class));
}
});
}
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
-import androidx.lifecycle.LifecycleOwner;
import com.ic.iclooger.ICLogger;
import com.ic.launcher.R;
initView();
}
- public void initViewModel(LifecycleOwner lifecycleOwner) {
+ public void initViewModel() {
ICLogger.d("initViewModel...");
mainPagerPresenter = MainPagePresenter.getInstance(getContext());
- mainPagerPresenter.dateValue.observe(lifecycleOwner, dateValue -> {
+ mainPagerPresenter.dateValue.observeForever(dateValue -> {
//ICLogger.d("dateValue == > " + dateValue);
dateView.setText(dateValue);
});
- mainPagerPresenter.timeValue.observe(lifecycleOwner, timeValue -> {
+ mainPagerPresenter.timeValue.observeForever(timeValue -> {
//ICLogger.d("timeValue == > " + timeValue);
timeView.setText(timeValue);
});
- mainPagerPresenter.weekValue.observe(lifecycleOwner, weekValue -> {
+ mainPagerPresenter.weekValue.observeForever(weekValue -> {
weekView.setText(weekValue);
});
- mainPagerPresenter.bodyTempValue.observe(lifecycleOwner, bodyTempValue -> {
+ mainPagerPresenter.bodyTempValue.observeForever(bodyTempValue -> {
bodyTempView.setText(bodyTempValue);
});
- mainPagerPresenter.envTempValue.observe(lifecycleOwner, envTempValue -> {
+ mainPagerPresenter.envTempValue.observeForever(envTempValue -> {
envTempView.setText(envTempValue);
});
- mainPagerPresenter.oxygenValue.observe(lifecycleOwner, oxygenValue -> {
+ mainPagerPresenter.oxygenValue.observeForever(oxygenValue -> {
bloodOxygenView.setText(oxygenValue);
});
- mainPagerPresenter.heartRate.observe(lifecycleOwner, heartRate -> {
+ mainPagerPresenter.heartRate.observeForever(heartRate -> {
heartRateView.setText(heartRate);
});
}
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+ android:shape="rectangle">
+
+ <solid android:color="#E5E5E5" />
+
+ <corners android:radius="10dp" />
+
+</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
-<androidx.constraintlayout.widget.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
- android:layout_height="match_parent">
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:background="#cf000000">
+
+ <ScrollView
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical">
+
+ <LinearLayout
+ android:id="@+id/notification_container"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:orientation="vertical"
+ android:paddingTop="30dp">
+
+ </LinearLayout>
+
+ </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:app="http://schemas.android.com/apk/res-auto"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_margin="5dp"
+ android:background="@drawable/shape_notification_item_bg"
+ android:padding="10dp">
+
+ <ImageView
+ android:id="@+id/left_icon"
+ android:layout_width="50dp"
+ android:layout_height="50dp"
+ android:src="@mipmap/icon_notification_blood_oxygen"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintLeft_toLeftOf="parent"
+ app:layout_constraintTop_toTopOf="parent" />
+
+ <TextView
+ android:id="@+id/notification_title"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginLeft="10dp"
+ android:text="通知标题"
+ android:textColor="@color/black"
+ android:textSize="22sp"
+ app:layout_constraintLeft_toRightOf="@id/left_icon"
+ app:layout_constraintTop_toTopOf="parent" />
+
+ <TextView
+ android:id="@+id/notification_time"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginLeft="10dp"
+ android:text="2021-3-23 12:09"
+ android:textColor="@color/black"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintLeft_toRightOf="@id/left_icon"
+ app:layout_constraintTop_toBottomOf="@id/notification_title" />
+
+ <ImageView
+ android:id="@+id/notification_dot"
+ android:layout_width="14dp"
+ android:layout_height="14dp"
+ android:src="@mipmap/icon_notification_dot_normal"
+ app:layout_constraintRight_toRightOf="parent"
+ app:layout_constraintTop_toTopOf="parent" />
+
+</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<!-- Customize your theme here. -->
<item name="android:windowBackground">@android:color/black</item>
</style>
+
+ <style name="ICNotification" parent="Theme.LoopPageLauncher">
+ <!-- 背景色透明 -->
+ <item name="android:windowBackground">@android:color/transparent</item>
+ <!-- 是否有透明属性 -->
+ <item name="android:windowIsTranslucent">true</item>
+ <!-- 背景是否半透明 -->
+ <item name="android:backgroundDimEnabled">false</item>
+ <!-- activity窗口切换效果 -->
+ <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
+ </style>
</resources>
\ No newline at end of file