├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── org
│ │ └── wangchenlong
│ │ └── timerappwidget
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── org
│ │ │ └── wangchenlong
│ │ │ └── timerappwidget
│ │ │ ├── MainActivity.java
│ │ │ ├── NotificationReceiver.java
│ │ │ └── TimerAppWidget.java
│ └── res
│ │ ├── drawable-hdpi
│ │ ├── avatar_jessica.png
│ │ ├── avatar_soo_young.png
│ │ ├── avatar_tiffany.png
│ │ ├── avatar_yoo_na.png
│ │ └── preview_widget.png
│ │ ├── drawable
│ │ ├── bkg_widget.xml
│ │ └── bkg_widget_btn.xml
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ ├── app_widget_timer.xml
│ │ ├── layout_timer.xml
│ │ └── notification_timer.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ ├── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ │ └── xml
│ │ └── timer_widget_provider.xml
│ └── test
│ └── java
│ └── org
│ └── wangchenlong
│ └── timerappwidget
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | Android
46 |
47 |
48 | Android Lint
49 |
50 |
51 | Java
52 |
53 |
54 | Java language level migration aidsJava
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 | $USER_HOME$/.subversion
78 |
79 |
80 |
81 |
82 |
83 | 1.8
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 实现同步更新的通知与插件
2 |
3 | > 欢迎Follow我的[GitHub](https://github.com/SpikeKing)
4 |
5 | 在Android中, 除了本应用的视图外, 还允许操作**远程视图(RemoteView)**. 其中包含两类实例, 一类是**通知(Notification)**, 一类是**插件(Widget)**, 这些都是附着于系统中, 通过广播更新页面. 系统为了避免频繁更新, 规定最低频率, 如果需要饶过这个机制, 则必须使用**定时器(Alarm)**. 本文连接定时器与远程视图, 达到实时更新的目的, 两者都会涉及PendingIntent的使用.
6 |
7 | ---
8 |
9 | ## 插件
10 |
11 | **插件(Widget)**实现两种功能, 一种是更新当前时间, 一种是更新启动状态, 需要注册两个**IntentFilter**. 使用Alarm定时器更新时间, 通过按钮控制启动状态.
12 |
13 | ### 注册
14 |
15 | **AppWidget**(插件)在本质上是**Receiver**, 注册在**AndroidManifest**中也是, 不同的是定义特定的``intent-filter``, 即``APPWIDGET_UPDATE``.
16 |
17 | ``` xml
18 |
19 | ```
20 |
21 | 并指定``meta-data``的``name``与``resource``. ``name``是固定的, ``resource``指明所使用的资源信息.
22 |
23 | ``` xml
24 |
25 |
26 |
27 |
28 |
29 |
32 |
33 | ```
34 |
35 | AppWidget的描述信息.
36 |
37 | ``` xml
38 |
45 | ```
46 |
47 | **initialLayout**: Widget的布局(Layout)文件.
48 |
49 | **minWidth** & **minHeight**: 定义Widget的最小宽度和高度, 当数值不是桌面cell的整数倍时, 宽高会被增至最接近的cell大小.
50 |
51 | **previewImage**: 当用户选择添加Widget时的预览图片. 如果未定义, 则展示应用的登录图标.
52 |
53 | **resizeMode**: 在水平和竖直方向是否允许调整大小, 值可选: horizontal(水平方向), vertical(竖直方向), none(不允许调整).
54 |
55 | ### 定义
56 |
57 | **AppWidget**继承于``AppWidgetProvider``, 而最终继承于``BroadcastReceiver``. ``onUpdate``负责更新显示数据.
58 |
59 | ``` java
60 | public class TimerAppWidget extends AppWidgetProvider {
61 | // 每次更新都会创建新的实例, 只能使用静态变量
62 | private static boolean sIsUpdate = false; // 是否启动更新时间
63 | private static int sImageIndex = 0;
64 | private static long sUpdateImageLastTime = 0L; // 上次更新图片的时间
65 |
66 | @Override public void onReceive(Context context, Intent intent) {
67 | super.onReceive(context, intent);
68 | if (intent.getAction().equals(CHANGE_STATE)) {
69 | sIsUpdate = !sIsUpdate;
70 |
71 | if (sIsUpdate) {
72 | startUpdate(context); // 开始更新
73 | } else {
74 | stopUpdate(context); // 结束更新
75 | }
76 | }
77 | }
78 |
79 | @Override
80 | public void onUpdate(Context context, AppWidgetManager appWidgetManager,
81 | int[] appWidgetIds) {
82 | super.onUpdate(context, appWidgetManager, appWidgetIds);
83 |
84 | RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.app_widget_timer);
85 | ComponentName widget = new ComponentName(context, TimerAppWidget.class);
86 |
87 | Date date = new Date();
88 |
89 | // 设置文字
90 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH);
91 | rv.setTextViewText(R.id.widget_tv_text, sdf.format(date)); // 设置文字
92 |
93 | // 更换图片
94 | long seconds = TimeUnit.MILLISECONDS.toSeconds(date.getTime());
95 | long interval = seconds - sUpdateImageLastTime; // 每隔5秒更换图片与图片文字
96 | if (sUpdateImageLastTime == 0 || interval % 5 == 0) {
97 | sImageIndex++;
98 | rv.setImageViewResource(R.id.widget_tv_image, mAvatars[sImageIndex % IMAGE_COUNT]); // 设置图片
99 | rv.setTextViewText(R.id.widget_tv_image_text, context.getString(mNames[sImageIndex % IMAGE_COUNT]));
100 | sUpdateImageLastTime = seconds;
101 | }
102 |
103 | // 点击头像跳转主页
104 | Intent mainIntent = new Intent(context, MainActivity.class);
105 | PendingIntent mainPi = PendingIntent.getActivity(context, 0, mainIntent, FLAG_UPDATE_CURRENT);
106 | rv.setOnClickPendingIntent(R.id.widget_tv_image, mainPi); // 设置点击事件
107 |
108 | // 开启或关闭时间的控制
109 | Intent changeIntent = new Intent(CHANGE_STATE);
110 | PendingIntent changePi = PendingIntent.getBroadcast(context, 0, changeIntent, FLAG_UPDATE_CURRENT);
111 | rv.setOnClickPendingIntent(R.id.widget_b_control, changePi);
112 |
113 | // 更新插件
114 | appWidgetManager.updateAppWidget(widget, rv);
115 | }
116 | }
117 | ```
118 |
119 | 获取当前的远程视图(``RemoteViews``)和组件信息(``ComponentName``), 最终用于更新插件, 即``updateAppWidget``.
120 |
121 | ``` java
122 | RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.app_widget_timer);
123 | ComponentName widget = new ComponentName(context, TimerAppWidget.class);
124 | // ...
125 | appWidgetManager.updateAppWidget(widget, rv);
126 | ```
127 |
128 | 在``RemoteViews``中设置显示文字(``TextView``)与图片(``ImageView``)的控件, 或者点击事件. 注意, 事件触发使用``PendingIntent``广播.
129 |
130 | ``` java
131 | rv.setTextViewText(R.id.widget_tv_text, sdf.format(date)); // 设置文字
132 | rv.setImageViewResource(R.id.widget_tv_image, mAvatars[(int) interval % 4]); // 设置图片
133 |
134 | Intent mainIntent = new Intent(context, MainActivity.class);
135 | PendingIntent mainPi = PendingIntent.getBroadcast(context, 0, mainIntent, FLAG_CANCEL_CURRENT);
136 | rv.setOnClickPendingIntent(R.id.widget_tv_image, mainPi); // 设置点击事件
137 | ```
138 |
139 | ### 更新
140 |
141 | 更新的**延迟消息**(``PendingIntent``), 获取组件与布局, 系统管理器更新插件, 获取插件ID组; 使用系统默认的插件活动``ACTION_APPWIDGET_UPDATE``与参数``EXTRA_APPWIDGET_IDS``设置延迟消息, 供**定时器**(``AlarmManager``)使用.
142 |
143 | ``` java
144 | private PendingIntent getUpdateIntent(Context context, boolean isStart) {
145 | // 获取当前的组件
146 | ComponentName widget = new ComponentName(context, TimerAppWidget.class);
147 |
148 | // 获取布局, 并设置关闭显示
149 | RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.app_widget_timer);
150 | if (isStart) {
151 | rv.setTextViewText(R.id.widget_b_control, context.getString(R.string.stop));
152 | } else {
153 | rv.setTextViewText(R.id.widget_b_control, context.getString(R.string.start));
154 | }
155 |
156 | // 获取系统的AppWidgetManager
157 | AppWidgetManager awm = AppWidgetManager.getInstance(context);
158 |
159 | // 更新页面组件
160 | awm.updateAppWidget(widget, rv);
161 | int appWidgetIds[] = awm.getAppWidgetIds(widget);
162 |
163 | Intent alertIntent = new Intent(context, TimerAppWidget.class);
164 | alertIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); // 设置更新活动
165 | alertIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); // 设置当前插件的ID
166 | return PendingIntent.getBroadcast(context, 0, alertIntent,
167 | FLAG_CANCEL_CURRENT); // 取消前一个更新
168 | }
169 | ```
170 |
171 | 启动或关闭定时器, 每秒更新一次.
172 |
173 | ``` java
174 | public void startUpdate(Context context) {
175 | AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
176 |
177 | // 设置更新
178 | am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
179 | 1000, getUpdateIntent(context, true));
180 | }
181 |
182 | public void stopUpdate(Context context) {
183 | AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
184 |
185 | // 取消更新
186 | am.cancel(getUpdateIntent(context, false));
187 | }
188 | ```
189 |
190 | ---
191 |
192 | ## 通知
193 |
194 | 通知实现与插件类似, 使用相同的控制广播, 保持同步.
195 |
196 | ### 定义
197 |
198 | 通知栏使用自定义布局, 设置头像跳转事件与状态修改事件, 发送相应的**PendingIntent**广播, 注意状态使用**FLAG_UPDATE_CURRENT**, 更新当前重复的Intent. 当与Widget的PendingIntent重复时, 进行相应的替换.
199 |
200 | ``` java
201 | public static NotificationCompat.Builder getNotification(Context context) {
202 | NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
203 | builder.setSmallIcon(R.drawable.avatar_jessica);
204 | builder.setWhen(System.currentTimeMillis());
205 | builder.setOngoing(true); // 始终存在
206 |
207 | // 开启或关闭时间的控制
208 | RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.notification_timer);
209 |
210 | // 点击更新状态按钮
211 | Intent changeIntent = new Intent(CHANGE_STATE);
212 | PendingIntent changePi = PendingIntent.getBroadcast(context, 0, changeIntent,
213 | PendingIntent.FLAG_UPDATE_CURRENT);
214 |
215 | // 点击头像跳转主页
216 | Intent mainIntent = new Intent(context, MainActivity.class);
217 | PendingIntent mainPi = PendingIntent.getActivity(context, 0, mainIntent, FLAG_UPDATE_CURRENT);
218 |
219 | rv.setOnClickPendingIntent(R.id.widget_tv_image, mainPi); // 设置头像
220 | rv.setOnClickPendingIntent(R.id.widget_b_control, changePi); // 设置更新
221 |
222 | builder.setCustomContentView(rv);
223 |
224 | return builder;
225 | }
226 | ```
227 |
228 | ### 更新
229 |
230 | 收到通知后, 实时更新当前时间, 每隔5秒循环更新图片, 由于远程视图, 每次都会创建实例, 因此参数需要使用静态变量.
231 |
232 | ``` java
233 | private void updateData(Context context) {
234 | NotificationCompat.Builder builder = getNotification(context);
235 |
236 | // 获取布局, 并设置关闭显示
237 | RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.notification_timer);
238 |
239 | Date date = new Date();
240 |
241 | // 设置文字
242 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH);
243 | rv.setTextViewText(R.id.widget_tv_text, sdf.format(date)); // 设置文字
244 |
245 | // 更换图片
246 | long seconds = TimeUnit.MILLISECONDS.toSeconds(date.getTime());
247 | long interval = seconds - sUpdateImageLastTime; // 每隔5秒更换图片与图片文字
248 | if (sUpdateImageLastTime == 0 || interval % 5 == 0) {
249 | sImageIndex++;
250 | rv.setImageViewResource(R.id.widget_tv_image, mAvatars[sImageIndex % IMAGE_COUNT]); // 设置图片
251 | rv.setTextViewText(R.id.widget_tv_image_text, context.getString(mNames[sImageIndex % IMAGE_COUNT]));
252 | sUpdateImageLastTime = seconds;
253 | }
254 |
255 | builder.setCustomContentView(rv);
256 | Notification notification = builder.build();
257 | NotificationManager manager = (NotificationManager)
258 | context.getSystemService(Context.NOTIFICATION_SERVICE);
259 | manager.notify(sId, notification);
260 | }
261 | ```
262 |
263 | ---
264 |
265 | 至此, 定时更新的插件与通知已经完成, 注意PendingIntent与定时器的使用方式. 这个实例对于开发插件与通知而言, 已经完全足够, 抛砖引玉.
266 |
267 | OK, that's all! Enjoy it!
268 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 24
5 | buildToolsVersion "24.0.3"
6 | defaultConfig {
7 | applicationId "org.wangchenlong.timerappwidget"
8 | minSdkVersion 16
9 | targetSdkVersion 24
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:24.2.1'
28 | testCompile 'junit:junit:4.12'
29 | }
30 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/wangchenlong/Installations/android-sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/org/wangchenlong/timerappwidget/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package org.wangchenlong.timerappwidget;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("org.wangchenlong.timerappwidget", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/java/org/wangchenlong/timerappwidget/MainActivity.java:
--------------------------------------------------------------------------------
1 | package org.wangchenlong.timerappwidget;
2 |
3 | import android.app.Notification;
4 | import android.app.NotificationManager;
5 | import android.content.Context;
6 | import android.os.Bundle;
7 | import android.support.v4.app.NotificationCompat;
8 | import android.support.v7.app.AppCompatActivity;
9 |
10 | import static org.wangchenlong.timerappwidget.NotificationReceiver.sId;
11 |
12 | /**
13 | * 主页, 直接启动通知栏
14 | *
15 | * @author C.L. Wang
16 | */
17 | public class MainActivity extends AppCompatActivity {
18 |
19 | @Override
20 | protected void onCreate(Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_main);
23 |
24 | Context context = getApplicationContext();
25 | initNotification(context); // 初始化通知栏
26 | }
27 |
28 | /**
29 | * 初始化通知栏
30 | *
31 | * @param context 上下文
32 | */
33 | private void initNotification(Context context) {
34 | NotificationCompat.Builder builder = NotificationReceiver.getNotification(context);
35 | Notification notification = builder.build();
36 | NotificationManager manager = (NotificationManager)
37 | context.getSystemService(Context.NOTIFICATION_SERVICE);
38 | manager.notify(sId, notification);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/app/src/main/java/org/wangchenlong/timerappwidget/NotificationReceiver.java:
--------------------------------------------------------------------------------
1 | package org.wangchenlong.timerappwidget;
2 |
3 | import android.app.AlarmManager;
4 | import android.app.Notification;
5 | import android.app.NotificationManager;
6 | import android.app.PendingIntent;
7 | import android.content.BroadcastReceiver;
8 | import android.content.Context;
9 | import android.content.Intent;
10 | import android.support.v4.app.NotificationCompat;
11 | import android.widget.RemoteViews;
12 |
13 | import java.text.SimpleDateFormat;
14 | import java.util.Date;
15 | import java.util.Locale;
16 | import java.util.concurrent.TimeUnit;
17 | import java.util.concurrent.atomic.AtomicInteger;
18 |
19 | import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
20 | import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
21 | import static org.wangchenlong.timerappwidget.TimerAppWidget.CHANGE_STATE;
22 | import static org.wangchenlong.timerappwidget.TimerAppWidget.IMAGE_COUNT;
23 | import static org.wangchenlong.timerappwidget.TimerAppWidget.mAvatars;
24 | import static org.wangchenlong.timerappwidget.TimerAppWidget.mNames;
25 |
26 | /**
27 | * 通知栏更新
28 | *
29 | * Created by wangchenlong on 16/12/13.
30 | */
31 | public class NotificationReceiver extends BroadcastReceiver {
32 | public static int sId = new AtomicInteger(0).incrementAndGet();
33 |
34 | // 修改通知栏状态
35 | public static final String NOTIFICATION_STATE =
36 | "org.wangchenlong.timerappwidget.action.NOTIFICATION_STATE";
37 |
38 | private static boolean sIsUpdate = false; // 是否启动更新时间
39 | private static long sUpdateImageLastTime = 0L; // 上次更新图片的时间
40 | private static int sImageIndex = 0; // 图片位置
41 |
42 | @Override public void onReceive(Context context, Intent intent) {
43 | if (intent == null) {
44 | return;
45 | }
46 |
47 | String action = intent.getAction();
48 | switch (action) {
49 | case CHANGE_STATE:
50 | setUpdate(context);
51 | break;
52 | case NOTIFICATION_STATE:
53 | updateData(context);
54 | break;
55 | default:
56 | break;
57 | }
58 | }
59 |
60 | /**
61 | * 设置更新状态
62 | *
63 | * @param context 上下文
64 | */
65 | private void setUpdate(Context context) {
66 | sIsUpdate = !sIsUpdate;
67 | if (sIsUpdate) {
68 | startUpdate(context); // 开始更新
69 | } else {
70 | stopUpdate(context); // 结束更新
71 | }
72 | }
73 |
74 | /**
75 | * 更新数据
76 | *
77 | * @param context 上下文
78 | */
79 | private void updateData(Context context) {
80 | NotificationCompat.Builder builder = getNotification(context);
81 |
82 | // 获取布局, 并设置关闭显示
83 | RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.notification_timer);
84 |
85 | Date date = new Date();
86 |
87 | // 设置文字
88 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH);
89 | rv.setTextViewText(R.id.widget_tv_text, sdf.format(date)); // 设置文字
90 |
91 | // 更换图片
92 | long seconds = TimeUnit.MILLISECONDS.toSeconds(date.getTime());
93 | long interval = seconds - sUpdateImageLastTime; // 每隔5秒更换图片与图片文字
94 | if (sUpdateImageLastTime == 0 || interval % 5 == 0) {
95 | sImageIndex++;
96 | rv.setImageViewResource(R.id.widget_tv_image, mAvatars[sImageIndex % IMAGE_COUNT]); // 设置图片
97 | rv.setTextViewText(R.id.widget_tv_image_text, context.getString(mNames[sImageIndex % IMAGE_COUNT]));
98 | sUpdateImageLastTime = seconds;
99 | }
100 |
101 | builder.setCustomContentView(rv);
102 | Notification notification = builder.build();
103 | NotificationManager manager = (NotificationManager)
104 | context.getSystemService(Context.NOTIFICATION_SERVICE);
105 | manager.notify(sId, notification);
106 | }
107 |
108 | /**
109 | * 设置通知栏
110 | *
111 | * @param context 上下文
112 | */
113 | public static NotificationCompat.Builder getNotification(Context context) {
114 | NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
115 | builder.setSmallIcon(R.drawable.avatar_jessica);
116 | builder.setWhen(System.currentTimeMillis());
117 | builder.setOngoing(true); // 始终存在
118 |
119 | // 开启或关闭时间的控制
120 | RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.notification_timer);
121 |
122 | // 点击更新状态按钮
123 | Intent changeIntent = new Intent(CHANGE_STATE);
124 | PendingIntent changePi = PendingIntent.getBroadcast(context, 0, changeIntent,
125 | PendingIntent.FLAG_UPDATE_CURRENT);
126 |
127 | // 点击头像跳转主页
128 | Intent mainIntent = new Intent(context, MainActivity.class);
129 | PendingIntent mainPi = PendingIntent.getActivity(context, 0, mainIntent, FLAG_UPDATE_CURRENT);
130 |
131 | rv.setOnClickPendingIntent(R.id.widget_tv_image, mainPi); // 设置头像
132 | rv.setOnClickPendingIntent(R.id.widget_b_control, changePi); // 设置更新
133 |
134 | builder.setCustomContentView(rv);
135 |
136 | return builder;
137 | }
138 |
139 | /**
140 | * 启动更新小插件的时间
141 | *
142 | * @param context 上下文
143 | */
144 | public void startUpdate(Context context) {
145 | AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
146 |
147 | // 设置更新
148 | am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
149 | 1000, getUpdateIntent(context, true));
150 | }
151 |
152 | /**
153 | * 停止更新小插件的时间
154 | *
155 | * @param context 上下文
156 | */
157 | public void stopUpdate(Context context) {
158 | AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
159 |
160 | // 取消更新
161 | am.cancel(getUpdateIntent(context, false));
162 | }
163 |
164 | /**
165 | * 获取需要更新的延迟消息
166 | *
167 | * @param context 上下文
168 | * @return 延迟消息
169 | */
170 | private PendingIntent getUpdateIntent(Context context, boolean isStart) {
171 | NotificationCompat.Builder builder = getNotification(context);
172 |
173 | RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.notification_timer);
174 | if (isStart) {
175 | rv.setTextViewText(R.id.widget_b_control, context.getString(R.string.stop));
176 | } else {
177 | rv.setTextViewText(R.id.widget_b_control, context.getString(R.string.start));
178 | }
179 | builder.setCustomContentView(rv);
180 |
181 | Notification notification = builder.build();
182 | NotificationManager manager = (NotificationManager)
183 | context.getSystemService(Context.NOTIFICATION_SERVICE);
184 | manager.notify(sId, notification);
185 |
186 | Intent alertIntent = new Intent(NOTIFICATION_STATE); // 设置更新活动
187 | return PendingIntent.getBroadcast(context, 0, alertIntent, FLAG_CANCEL_CURRENT); // 取消前一个更新
188 | }
189 | }
190 |
--------------------------------------------------------------------------------
/app/src/main/java/org/wangchenlong/timerappwidget/TimerAppWidget.java:
--------------------------------------------------------------------------------
1 | package org.wangchenlong.timerappwidget;
2 |
3 | import android.app.AlarmManager;
4 | import android.app.PendingIntent;
5 | import android.appwidget.AppWidgetManager;
6 | import android.appwidget.AppWidgetProvider;
7 | import android.content.ComponentName;
8 | import android.content.Context;
9 | import android.content.Intent;
10 | import android.support.annotation.DrawableRes;
11 | import android.support.annotation.StringRes;
12 | import android.widget.RemoteViews;
13 |
14 | import java.text.SimpleDateFormat;
15 | import java.util.Date;
16 | import java.util.Locale;
17 | import java.util.concurrent.TimeUnit;
18 |
19 | import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
20 | import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
21 |
22 | /**
23 | * 实时更新数据的小控件, 每隔2秒更换图片, 显示时间.
24 | *
25 | * Created by wangchenlong on 16/12/2.
26 | */
27 | public class TimerAppWidget extends AppWidgetProvider {
28 |
29 | // 更新小插件的广播
30 | public static final String CHANGE_STATE = "org.wangchenlong.timerappwidget.action.CHANGE_STATE";
31 |
32 | // 每次更新都会创建新的实例, 只能使用静态变量
33 | private static boolean sIsUpdate = false; // 是否启动更新时间
34 | private static int sImageIndex = 0;
35 | private static long sUpdateImageLastTime = 0L; // 上次更新图片的时间
36 |
37 | public static int IMAGE_COUNT = 4; // 图片数量
38 |
39 | @DrawableRes
40 | public static final int[] mAvatars = new int[]{
41 | R.drawable.avatar_tiffany,
42 | R.drawable.avatar_jessica,
43 | R.drawable.avatar_soo_young,
44 | R.drawable.avatar_yoo_na
45 | };
46 |
47 | @StringRes
48 | public static final int[] mNames = new int[]{
49 | R.string.tiffany,
50 | R.string.jessica,
51 | R.string.soo_young,
52 | R.string.yoo_na
53 | };
54 |
55 | @Override public void onReceive(Context context, Intent intent) {
56 | super.onReceive(context, intent);
57 | if (intent.getAction().equals(CHANGE_STATE)) {
58 | sIsUpdate = !sIsUpdate;
59 |
60 | if (sIsUpdate) {
61 | startUpdate(context); // 开始更新
62 | } else {
63 | stopUpdate(context); // 结束更新
64 | }
65 | }
66 | }
67 |
68 | /**
69 | * 启动更新小插件的时间
70 | *
71 | * @param context 上下文
72 | */
73 | public void startUpdate(Context context) {
74 | AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
75 |
76 | // 设置更新
77 | am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
78 | 1000, getUpdateIntent(context, true));
79 | }
80 |
81 | /**
82 | * 停止更新小插件的时间
83 | *
84 | * @param context 上下文
85 | */
86 | public void stopUpdate(Context context) {
87 | AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
88 |
89 | // 取消更新
90 | am.cancel(getUpdateIntent(context, false));
91 | }
92 |
93 | /**
94 | * 获取需要更新的延迟消息
95 | *
96 | * @param context 上下文
97 | * @return 延迟消息
98 | */
99 | private PendingIntent getUpdateIntent(Context context, boolean isStart) {
100 | // 获取当前的组件
101 | ComponentName widget = new ComponentName(context, TimerAppWidget.class);
102 |
103 | // 获取布局, 并设置关闭显示
104 | RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.app_widget_timer);
105 | if (isStart) {
106 | rv.setTextViewText(R.id.widget_b_control, context.getString(R.string.stop));
107 | } else {
108 | rv.setTextViewText(R.id.widget_b_control, context.getString(R.string.start));
109 | }
110 |
111 | // 获取系统的AppWidgetManager
112 | AppWidgetManager awm = AppWidgetManager.getInstance(context);
113 |
114 | // 更新页面组件
115 | awm.updateAppWidget(widget, rv);
116 | int appWidgetIds[] = awm.getAppWidgetIds(widget);
117 |
118 | Intent alertIntent = new Intent(context, TimerAppWidget.class);
119 | alertIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); // 设置更新活动
120 | alertIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); // 设置当前插件的ID
121 | return PendingIntent.getBroadcast(context, 0, alertIntent,
122 | FLAG_CANCEL_CURRENT); // 取消前一个更新
123 | }
124 |
125 | @Override
126 | public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
127 | super.onUpdate(context, appWidgetManager, appWidgetIds);
128 |
129 | RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.app_widget_timer);
130 | ComponentName widget = new ComponentName(context, TimerAppWidget.class);
131 |
132 | Date date = new Date();
133 |
134 | // 设置文字
135 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH);
136 | rv.setTextViewText(R.id.widget_tv_text, sdf.format(date)); // 设置文字
137 |
138 | // 更换图片
139 | long seconds = TimeUnit.MILLISECONDS.toSeconds(date.getTime());
140 | long interval = seconds - sUpdateImageLastTime; // 每隔5秒更换图片与图片文字
141 | if (sUpdateImageLastTime == 0 || interval % 5 == 0) {
142 | sImageIndex++;
143 | rv.setImageViewResource(R.id.widget_tv_image, mAvatars[sImageIndex % IMAGE_COUNT]); // 设置图片
144 | rv.setTextViewText(R.id.widget_tv_image_text, context.getString(mNames[sImageIndex % IMAGE_COUNT]));
145 | sUpdateImageLastTime = seconds;
146 | }
147 |
148 | // 点击头像跳转主页
149 | Intent mainIntent = new Intent(context, MainActivity.class);
150 | PendingIntent mainPi = PendingIntent.getActivity(context, 0, mainIntent, FLAG_UPDATE_CURRENT);
151 | rv.setOnClickPendingIntent(R.id.widget_tv_image, mainPi); // 设置点击事件
152 |
153 | // 开启或关闭时间的控制
154 | Intent changeIntent = new Intent(CHANGE_STATE);
155 | PendingIntent changePi = PendingIntent.getBroadcast(context, 0, changeIntent, FLAG_UPDATE_CURRENT);
156 | rv.setOnClickPendingIntent(R.id.widget_b_control, changePi);
157 |
158 | // 更新插件
159 | appWidgetManager.updateAppWidget(widget, rv);
160 | }
161 | }
162 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/avatar_jessica.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpikeKing/TimerAppWidget/db7438504a82f7f3a12b00bd593e558d194ac9cb/app/src/main/res/drawable-hdpi/avatar_jessica.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/avatar_soo_young.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpikeKing/TimerAppWidget/db7438504a82f7f3a12b00bd593e558d194ac9cb/app/src/main/res/drawable-hdpi/avatar_soo_young.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/avatar_tiffany.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpikeKing/TimerAppWidget/db7438504a82f7f3a12b00bd593e558d194ac9cb/app/src/main/res/drawable-hdpi/avatar_tiffany.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/avatar_yoo_na.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpikeKing/TimerAppWidget/db7438504a82f7f3a12b00bd593e558d194ac9cb/app/src/main/res/drawable-hdpi/avatar_yoo_na.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/preview_widget.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpikeKing/TimerAppWidget/db7438504a82f7f3a12b00bd593e558d194ac9cb/app/src/main/res/drawable-hdpi/preview_widget.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bkg_widget.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bkg_widget_btn.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/app_widget_timer.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_timer.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
18 |
19 |
26 |
27 |
38 |
39 |
47 |
48 |
49 |
50 |
60 |
61 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/notification_timer.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpikeKing/TimerAppWidget/db7438504a82f7f3a12b00bd593e558d194ac9cb/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpikeKing/TimerAppWidget/db7438504a82f7f3a12b00bd593e558d194ac9cb/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpikeKing/TimerAppWidget/db7438504a82f7f3a12b00bd593e558d194ac9cb/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpikeKing/TimerAppWidget/db7438504a82f7f3a12b00bd593e558d194ac9cb/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpikeKing/TimerAppWidget/db7438504a82f7f3a12b00bd593e558d194ac9cb/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 | #FF6EB4
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | TimerAppWidget
3 | 计时部件(插件/工具)已安装, 请添加至桌面观看.
4 | 开始更新
5 |
6 |
7 | Tiffany!
8 | Jessica!
9 | SooYoung!
10 | YooNa!
11 |
12 | START
13 | STOP
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/timer_widget_provider.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/test/java/org/wangchenlong/timerappwidget/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package org.wangchenlong.timerappwidget;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.2.2'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SpikeKing/TimerAppWidget/db7438504a82f7f3a12b00bd593e558d194ac9cb/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------