public void onReceive(Context context, Intent intent) { Log.d("TimeSetReceiver", "onReceive"); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget); ComponentName thisWidget; // 组件名 AppWidgetManager manager; // AppWidget管理器 thisWidget = new ComponentName(context, CalendarWidget.class); manager = AppWidgetManager.getInstance(context); Calendar calendar = Calendar.getInstance(); int day = calendar.get(Calendar.DAY_OF_MONTH); int month = calendar.get(Calendar.MONTH) + 1; int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; Lunar lunar = new Lunar(calendar); Log.d("TimeSetReceiver", month + "," + day + lunar.toString()); String month_week = month + "月" + " 周" + weekArray[week]; views.setTextViewText(R.id.month_week, month_week); views.setTextViewText(R.id.nongli, lunar.toString()); views.setTextViewText(R.id.day, Html.fromHtml("<B>" + String.valueOf(day) + "</B>")); manager.updateAppWidget(thisWidget, views); Log.d("TimeSetReceiver", "updateAppWidget finish"); }
/** check Chinese new year */ private boolean checkChineseYear(int month, int day) { // ¼ÆËã³ýϦ Calendar calendar = Calendar.getInstance(); calendar.set(calendar.get(Calendar.YEAR), month - 1, day + 1); Lunar lunar = new Lunar(calendar); String str = lunar.getNumericMD(); if (str.equals("0101")) { return true; } return false; }
/** The key method of <code>Cell</code>: how to draw date number on the cell. */ @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (isHeader()) return; // Do not render the header if (day > 0) { AttributedString dayAttrStr; Font font = MonthCalendarImpl.this.getFont(); try { calendar.set(Calendar.DATE, day); String dayStr = Integer.toString(day); dayAttrStr = new AttributedString(dayStr); dayAttrStr.addAttribute( TextAttribute.FONT, isToday(calendar) ? font.deriveFont(Font.BOLD, font.getSize() + SHRINK + SHRINK) : font.deriveFont(Font.PLAIN, font.getSize() + SHRINK + 3)); Graphics2D g2 = (Graphics2D) g; TextLayout textLayout = new TextLayout(dayAttrStr.getIterator(), g2.getFontRenderContext()); int width = textLayout.getBounds().getBounds().width; Dimension size = getSize(); int x = size.width > width ? (size.width - width) / 2 : 0; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Draw in the center g2.drawString(dayAttrStr.getIterator(), x, (int) (textLayout.getAscent())); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); String normalDate = (month < 10 ? "0" + month : month) + "" + (day < 10 ? "0" + day : day); String lunarInfo = ""; if (checkNormalFestival(normalDate)) { lunarInfo = CodeConstants.NORMAL_FESTIVAL.get(normalDate); setForeground(Color.blue); } else if (checkDefineFestival(normalDate)) { lunarInfo = CodeConstants.DEFINE_FESTIVAL.get(normalDate); setForeground(Color.green); } else if (checkChineseYear(month, day)) { lunarInfo = CodeConstants.CHINESE_FESTIVAL.get("1299"); setForeground(Color.red); } else { // lunar date Lunar lunar = new Lunar(calendar); String lunarDate = lunar.getNumericMD(); if (checkChineseFestival(lunarDate)) { lunarInfo = CodeConstants.CHINESE_FESTIVAL.get(lunarDate); setForeground(Color.MAGENTA); } else { if (checkUnWorkFestival(normalDate + "W") || checkUnWorkFestival(lunarDate + "Y")) { setForeground(Color.red); } lunarInfo = lunar.toString(); } } AttributedString lunarStr = new AttributedString(lunarInfo); lunarStr.addAttribute( TextAttribute.FONT, isToday(calendar) ? font.deriveFont(Font.PLAIN, font.getSize()) : font); TextLayout textLayout2 = new TextLayout(lunarStr.getIterator(), g2.getFontRenderContext()); // Resume date calendar.set(Calendar.DATE, todayInMonth); int width2 = textLayout2.getBounds().getBounds().width; int x2 = size.width > width ? (size.width - width2) / 2 : 0; g2.drawString( lunarStr.getIterator(), x2, (int) (textLayout.getAscent() + textLayout.getDescent() + textLayout.getLeading() + textLayout2.getAscent())); } catch (ParseException e) { e.printStackTrace(); } } }
/* 获取农历字符串子程序 */ private void setText(){ synchronized (Main.class) { for (TextView dateView : mDateViews) { /* 判断当前日期栏是否包含上次更新后的日期文本 * 如果当前日期已经改变,则必须重新计算农历 * 如果当前日期未改变,则只需要重新用已经缓存的文本写入TextView */ //判断日期是否改变,不改变则不更新内容,改变则重新计算农历 String nDate = dateView.getText().toString(); String lDate = (String) XposedHelpers.getAdditionalInstanceField(dateView, "lDate"); String lunarText = (String) XposedHelpers.getAdditionalInstanceField(dateView, "lunarText"); String finalText = (String) XposedHelpers.getAdditionalInstanceField(dateView, "finalText"); if (lunarText == null || !(nDate.contains(lunarText) || finalText == null || nDate.equals(finalText))) { if (lDate == null || !nDate.equals(lDate)) { //获取时间 lunar.init(); //修正layout的singleLine属性 if (!layout_run) { try { //去掉singleLine属性 if (_layout_line) dateView.setSingleLine(false); //去除singleLine属性 //去掉align_baseline,并将其设置为center_vertical if (_layout_align) { //一般机型的状态栏都是RelativeLayout,少数为LinearLayout,但似乎影响不大 RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) dateView.getLayoutParams(); layoutParams.addRule(RelativeLayout.ALIGN_BASELINE, 0); //去除baseline对齐属性 layoutParams.addRule(RelativeLayout.CENTER_VERTICAL); //并将其设置为绝对居中 dateView.setLayoutParams(layoutParams); //设置布局参数 } //设置宽度为wrap_content if (_layout_width) { RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) dateView.getLayoutParams(); layoutParams.width = LayoutParams.WRAP_CONTENT; //取消宽度限制 dateView.setLayoutParams(layoutParams); } //设置高度为wrap_content if (_layout_height) { RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) dateView.getLayoutParams(); layoutParams.height = LayoutParams.WRAP_CONTENT; //取消高度限制 dateView.setLayoutParams(layoutParams); } layout_run = true; //已经执行过布局的处理步骤,下次不再执行 } catch (Throwable t) { XposedBridge.log("xStatusBarLunarDate: Statusbar layout fix error:"); XposedBridge.log(t); } } //更新记录的日期 XposedHelpers.setAdditionalInstanceField(dateView, "lDate", nDate); //从Lunar类中获得组合好的农历日期字符串(包括各节日) lunarText = lunar.getFormattedDate(_custom_format, _format); XposedHelpers.setAdditionalInstanceField(dateView, "lunarText", lunarText); //重置提醒次数 _notify_times = _notify_times_setting; if (_notify > 1) { //当天是否是节日 isFest = !"".equals(lunar.getFormattedDate("ff", 5)); if ((isFest || _notify == 2) && _lang != 3) { lunarTextToast = nDate.trim().replaceAll("\\n", " ") + "\n" + (_format == 5 ? lunar.getFormattedDate("", 3) : lunarText); } else { lunarTextToast = ""; } } //组合最终显示的文字 finalText = _remove_all ? lunarText : (_remove ? nDate.trim().replaceAll("[\\n|\\r]", " ") : nDate) + (_breakline ? "\n" : " ") + lunarText; XposedHelpers.setAdditionalInstanceField(dateView, "finalText", finalText); } //向TextView设置显示的最终文字 dateView.setText(finalText); } } } }