1111import android .net .Uri ;
1212import android .os .Environment ;
1313import android .text .TextUtils ;
14- import android .view .Gravity ;
15- import android .view .LayoutInflater ;
1614import android .view .View ;
17- import android .view .ViewGroup ;
18- import android .widget .PopupWindow ;
19- import android .widget .RelativeLayout ;
2015import android .widget .RemoteViews ;
21- import android .widget .TextView ;
2216import android .widget .Toast ;
2317
2418import org .xutils .common .Callback ;
3024
3125import luo .library .R ;
3226import luo .library .base .base .BaseAndroid ;
27+ import luo .library .base .widget .BaseDialog ;
3328
3429/**
3530 * 版本更新
3631 */
3732
3833public class UpdateManager {
39- public String DOWNLOAD_PATH = Environment .getExternalStorageDirectory ().getAbsolutePath () + "/downloads/" ;
34+ public String downLoadPath = Environment .getExternalStorageDirectory ().getAbsolutePath () + "/downloads/" ;
4035 public int type = 0 ;//更新方式,0:引导更新,1:安装更新,2:强制更新
4136 public String url = "" ;//apk下载地址
4237 public String updateMessage = "" ;//更新内容
43- public View view ;//activity根布局的view
4438 public String fileName = null ;//文件名
39+ public boolean isDownload = false ;//是否下载
4540 public Notification notification ;
4641 public RemoteViews contentView ;
4742 public NotificationManager notificationManager ;
43+ public BaseDialog dialog ;
4844
4945 public static UpdateManager updateManager ;
5046
@@ -59,119 +55,63 @@ private UpdateManager() {
5955
6056 }
6157
62- public UpdateManager setUrl (String url ) {
63- this .url = url ;
64- return this ;
65- }
66-
67-
68- public UpdateManager setType (int type ) {
69- this .type = type ;
70- return this ;
71- }
72-
73-
74- public UpdateManager setUpdateMessage (String updateMessage ) {
75- this .updateMessage = updateMessage ;
76- return this ;
77- }
78-
79-
80- public UpdateManager setView (View view ) {
81- this .view = view ;
82- return this ;
83- }
84-
8558 /**
8659 * 弹出版本更新提示框
8760 */
88- public void showPop (final Context context ) {
89- View contentView = LayoutInflater .from (context ).inflate (R .layout .base_dialog , null );
90- final PopupWindow popupWindow = new PopupWindow (contentView , ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT , true );
91- final TextView title = (TextView ) contentView .findViewById (R .id .tv_title );
92- TextView left = (TextView ) contentView .findViewById (R .id .tv_left );
93- TextView right = (TextView ) contentView .findViewById (R .id .tv_right );
94- TextView message = (TextView ) contentView .findViewById (R .id .tv_message );
95- RelativeLayout relativeLayout = (RelativeLayout ) contentView .findViewById (R .id .rl );
96-
97- //根据更新方式显示不同的文案
98- if (type == 0 ) {
99- title .setText ("发现新版本" );
100- left .setText ("立即更新" );
61+ public void showDialog (final Context context ) {
62+ String title = "" ;
63+ String left = "" ;
64+ boolean cancelable = true ;
65+ if (type == 1 | isDownload ) {
66+ title = "安装新版本" ;
67+ left = "立即安装" ;
10168 } else {
102- title . setText ( "安装新版本" ) ;
103- left . setText ( "立即安装" ) ;
69+ title = "发现新版本" ;
70+ left = "立即更新" ;
10471 }
105- right .setText ("取消" );
106- message .setText (updateMessage );
107- //升级按钮,根据更新方式来做不同的操作,0和2是下载apk并显示通知栏,1是直接安装apk
108- left .setOnClickListener (new View .OnClickListener () {
109- @ Override
110- public void onClick (View view ) {
111- popupWindow .dismiss ();
112- if (type == 0 | type == 2 ) {
113- if (url != null && !TextUtils .isEmpty (url )) {
114- createNotification (context );
115- downloadFile (context , true );
116- } else {
117- Toast .makeText (context , "下载地址错误" , Toast .LENGTH_SHORT ).show ();
118- }
119- } else {
120- installApk (context , new File (DOWNLOAD_PATH , fileName ));
121- }
122-
123- }
124- });
125- //取消按钮,当更新方式为强制更新时,直接退出
126- right .setOnClickListener (new View .OnClickListener () {
127- @ Override
128- public void onClick (View view ) {
129- popupWindow .dismiss ();
130- if (type == 2 ) {
131- System .exit (0 );
132- }
133- }
134- });
135- //点击外边区域消失窗口
136- if (type != 2 ) {
137- relativeLayout .setOnClickListener (new View .OnClickListener () {
138- @ Override
139- public void onClick (View view ) {
140- popupWindow .dismiss ();
141- }
142- });
72+ if (type == 2 ) {
73+ cancelable = false ;
14374 }
144- popupWindow .showAtLocation (view , Gravity .CENTER , 0 , 0 );
75+ dialog = new BaseDialog .Builder (context ).setTitle (title ).setMessage (updateMessage ).setCancelable (cancelable )
76+ .setLeftClick (left , new View .OnClickListener () {
77+ @ Override
78+ public void onClick (View view ) {
79+ dialog .dismiss ();
80+ if (type == 1 | isDownload ) {
81+ installApk (context , new File (downLoadPath , fileName ));
82+ } else {
83+ if (url != null && !TextUtils .isEmpty (url )) {
84+ createNotification (context );
85+ downloadFile (context , true );
86+ } else {
87+ Toast .makeText (context , "下载地址错误" , Toast .LENGTH_SHORT ).show ();
88+ }
89+ }
90+ }
91+ })
92+ .setRightClick ("取消" , new View .OnClickListener () {
93+ @ Override
94+ public void onClick (View view ) {
95+ dialog .dismiss ();
96+ if (type == 2 ) {
97+ System .exit (0 );
98+ }
99+ }
100+ })
101+ .create ();
102+ dialog .show ();
145103 }
146104
147105
148106 /**
149107 * 下载apk
108+ *
150109 * @param context
151- * @param installApk 下载完成后是否需要安装
110+ * @param installApk 下载完成后是否立即安装
152111 */
153112 public void downloadFile (final Context context , final boolean installApk ) {
154- File dir = new File (DOWNLOAD_PATH );
155- if (!dir .exists ()) {
156- dir .mkdir ();
157- }
158- //截取apk的文件名
159- fileName = url .substring (url .lastIndexOf ("/" ) + 1 , url .length ());
160- if (fileName == null && TextUtils .isEmpty (fileName ) && !fileName .contains (".apk" )) {
161- fileName = context .getPackageName () + ".apk" ;
162- }
163- //判断是否存在了这个apk,存在了就不下载了,直接安装或者弹框提示
164- File file = new File (DOWNLOAD_PATH + fileName );
165- if (file .exists ()) {
166- if (installApk ) {
167- installApk (context , new File (DOWNLOAD_PATH , fileName ));
168- } else {
169- showPop (context );
170- }
171- return ;
172- }
173113 RequestParams params = new RequestParams (url );
174- params .setSaveFilePath (DOWNLOAD_PATH + fileName );
114+ params .setSaveFilePath (downLoadPath + fileName );
175115 x .http ().request (HttpMethod .GET , params , new Callback .ProgressCallback <File >() {
176116
177117 @ Override
@@ -181,7 +121,7 @@ public void onSuccess(File result) {
181121
182122 @ Override
183123 public void onError (Throwable ex , boolean isOnCallback ) {
184- Toast .makeText (context , "网络错误" , Toast .LENGTH_SHORT ).show ();
124+ Toast .makeText (context , ex . getMessage () , Toast .LENGTH_SHORT ).show ();
185125 }
186126
187127 @ Override
@@ -211,10 +151,11 @@ public void onLoading(long total, long current, boolean isDownloading) {
211151 notifyNotification (current , total );
212152 }
213153 if (total == current ) {
154+ notificationManager .cancelAll ();
214155 if (installApk ) {
215- installApk (context , new File (DOWNLOAD_PATH , fileName ));
156+ installApk (context , new File (downLoadPath , fileName ));
216157 } else {
217- showPop (context );
158+ showDialog (context );
218159 }
219160 }
220161 }
@@ -236,14 +177,14 @@ public void createNotification(Context context) {
236177 notification .contentView = contentView ;
237178 notification .flags = Notification .DEFAULT_ALL ;
238179 notificationManager = (NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
239- notificationManager .notify (R . layout . notification_item , notification );
180+ notificationManager .notify (0 , notification );
240181 }
241182
242183 public void notifyNotification (long percent , long length ) {
243184 contentView .setTextViewText (R .id .tv_progress , (percent * 100 / length ) + "%" );
244185 contentView .setProgressBar (R .id .progress , (int ) length , (int ) percent , false );
245186 notification .contentView = contentView ;
246- notificationManager .notify (R . layout . notification_item , notification );
187+ notificationManager .notify (0 , notification );
247188 }
248189
249190 /**
@@ -282,11 +223,35 @@ public boolean isWifi(Context mContext) {
282223 ConnectivityManager connectivityManager = (ConnectivityManager ) mContext
283224 .getSystemService (Context .CONNECTIVITY_SERVICE );
284225 NetworkInfo activeNetInfo = connectivityManager .getActiveNetworkInfo ();
285- if (activeNetInfo != null
286- && activeNetInfo .getType () == ConnectivityManager .TYPE_WIFI ) {
226+ if (activeNetInfo != null && activeNetInfo .getType () == ConnectivityManager .TYPE_WIFI ) {
287227 return true ;
288228 }
289229 return false ;
290230 }
291231
232+ public UpdateManager setUrl (String url ) {
233+ this .url = url ;
234+ return this ;
235+ }
236+
237+ public UpdateManager setType (int type ) {
238+ this .type = type ;
239+ return this ;
240+ }
241+
242+ public UpdateManager setUpdateMessage (String updateMessage ) {
243+ this .updateMessage = updateMessage ;
244+ return this ;
245+ }
246+
247+ public UpdateManager setFileName (String fileName ) {
248+ this .fileName = fileName ;
249+ return this ;
250+ }
251+
252+ public UpdateManager setIsDownload (boolean download ) {
253+ isDownload = download ;
254+ return this ;
255+ }
256+
292257}
0 commit comments