From f037e1cb120b2758d535bbd90fac40581c9c70fa Mon Sep 17 00:00:00 2001 From: Administrator <981964879@qq.com> Date: Wed, 22 Apr 2020 18:23:50 +0800 Subject: [PATCH] =?UTF-8?q?update:2019.04.22=20fix:=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E7=A1=AE=E8=AE=A4=20add:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 10 +- app/src/main/AndroidManifest.xml | 4 +- .../main/java/com/info/sn/MainActivity.java | 28 +- .../main/java/com/info/sn/MyApplication.java | 146 +++++++++-- .../JWebSocketClient.java | 2 +- .../java/com/info/sn/network/UrlPath.java | 5 +- .../APKinstallReceiver.java | 2 +- .../{service => receiver}/BootReceiver.java | 6 +- .../info/sn/service/MyDownloadService.java | 39 ++- .../java/com/info/sn/service/StepService.java | 1 + .../java/com/info/sn/view/CustomDialog.java | 241 ++++++++++++++++++ .../java/com/info/sn/view/InfoDialog.java | 109 ++++++++ app/src/main/res/drawable/bg_dialog.xml | 9 + app/src/main/res/drawable/bind.png | Bin 0 -> 3065 bytes app/src/main/res/layout/custom_dialog.xml | 108 ++++++++ app/src/main/res/layout/dialog.xml | 75 ++++++ app/src/main/res/values/styles.xml | 12 + 17 files changed, 766 insertions(+), 31 deletions(-) rename app/src/main/java/com/info/sn/{service => network}/JWebSocketClient.java (96%) rename app/src/main/java/com/info/sn/{service => receiver}/APKinstallReceiver.java (95%) rename app/src/main/java/com/info/sn/{service => receiver}/BootReceiver.java (91%) create mode 100644 app/src/main/java/com/info/sn/view/CustomDialog.java create mode 100644 app/src/main/java/com/info/sn/view/InfoDialog.java create mode 100644 app/src/main/res/drawable/bg_dialog.xml create mode 100644 app/src/main/res/drawable/bind.png create mode 100644 app/src/main/res/layout/custom_dialog.xml create mode 100644 app/src/main/res/layout/dialog.xml diff --git a/app/build.gradle b/app/build.gradle index 2ba8f43..c8dbb53 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,8 +15,8 @@ android { applicationId "com.info.sn" minSdkVersion 23 targetSdkVersion 28 - versionCode 17 - versionName "1.1.7" + versionCode 18 + versionName "1.1.8" multiDexEnabled true testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" ndk { @@ -132,9 +132,9 @@ dependencies { implementation 'com.alibaba:fastjson:1.2.21' -// implementation 'com.blankj:utilcode:1.26.0' - implementation 'com.arialyy.aria:core:3.7.10' - annotationProcessor 'com.arialyy.aria:compiler:3.7.10' + implementation 'com.blankj:utilcode:1.26.0' + implementation 'com.arialyy.aria:core:3.8.5' + annotationProcessor 'com.arialyy.aria:compiler:3.8.5' implementation "org.java-websocket:Java-WebSocket:1.4.1" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ef0c23d..a43c8b2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -40,7 +40,7 @@ android:supportsRtl="true" android:theme="@style/AppTheme"> @@ -167,7 +167,7 @@ diff --git a/app/src/main/java/com/info/sn/MainActivity.java b/app/src/main/java/com/info/sn/MainActivity.java index 286c837..2fc3662 100644 --- a/app/src/main/java/com/info/sn/MainActivity.java +++ b/app/src/main/java/com/info/sn/MainActivity.java @@ -62,6 +62,7 @@ import static com.info.sn.jpush.TagAliasOperatorHelper.TagAliasBean; import static com.info.sn.jpush.TagAliasOperatorHelper.sequence; public class MainActivity extends AppCompatActivity { + public static final String REFRESHACTION = BuildConfig.APPLICATION_ID + ".REFRESH"; public static boolean isForeground = false; private ImageView imageView, exit; private TextView tv_note, tv_devsn, tv_username, tv_school, tv_grade, tv_version; @@ -70,6 +71,8 @@ public class MainActivity extends AppCompatActivity { private int DeviceInfo; private long mExitTime; + private RefreshReceiver mRefreshReceiver; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -82,6 +85,7 @@ public class MainActivity extends AppCompatActivity { pm.setApplicationEnabledSetting("com.info.sn", PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0); requestPermission(); registerMessageReceiver(); // used for receive msg + registerRefreshReceiver(); String rid = JPushInterface.getRegistrationID(getApplicationContext()); if (!rid.isEmpty()) { ToastUtil.debugShow("RegId:" + rid); @@ -98,6 +102,23 @@ public class MainActivity extends AppCompatActivity { startService(new Intent(MainActivity.this, MyDownloadService.class)); } + public void registerRefreshReceiver() { + mRefreshReceiver = new RefreshReceiver(); + IntentFilter filter = new IntentFilter(); + filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); + filter.addAction(REFRESHACTION); + registerReceiver(mRefreshReceiver, filter); + } + + public class RefreshReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + if (REFRESHACTION.equals(intent.getAction())) { + HTTPInterface.checkDevicesInfo(handler); + } + } + } + private void initView() { tv_note = findViewById(R.id.tv_note); imageView = findViewById(R.id.imageView); @@ -491,7 +512,7 @@ public class MainActivity extends AppCompatActivity { //for receive customer msg from jpush server private MessageReceiver mMessageReceiver; - public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION"; + public static final String MESSAGE_RECEIVED_ACTION = "com.info.sn.MESSAGE_RECEIVED_ACTION"; public static final String KEY_TITLE = "title"; public static final String KEY_MESSAGE = "message"; public static final String KEY_EXTRAS = "extras"; @@ -524,4 +545,9 @@ public class MainActivity extends AppCompatActivity { } } + @Override + protected void onDestroy() { + super.onDestroy(); + unregisterReceiver(mRefreshReceiver); + } } diff --git a/app/src/main/java/com/info/sn/MyApplication.java b/app/src/main/java/com/info/sn/MyApplication.java index d2c8acc..c237181 100644 --- a/app/src/main/java/com/info/sn/MyApplication.java +++ b/app/src/main/java/com/info/sn/MyApplication.java @@ -1,31 +1,49 @@ package com.info.sn; +import android.annotation.SuppressLint; import android.app.ActivityManager; +import android.app.AlertDialog; import android.app.Application; import android.app.usage.UsageStats; import android.app.usage.UsageStatsManager; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.os.Environment; import android.provider.Settings; import android.util.Log; +import android.view.View; +import android.view.WindowManager; +import android.widget.Toast; import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.task.DownloadTask; +import com.blankj.utilcode.util.ToastUtils; +import com.info.sn.jpush.LocalBroadcastManager; +import com.info.sn.network.UrlPath; import com.info.sn.network.api.HTTPInterface; +import com.info.sn.service.MyDownloadService; import com.info.sn.utils.ApkUtils; import com.info.sn.utils.LogUtils; import com.info.sn.utils.SPUtils; import com.info.sn.utils.ToastUtil; +import com.info.sn.utils.Utils; +import com.info.sn.view.CustomDialog; +import com.info.sn.view.InfoDialog; import com.lzy.okgo.OkGo; +import com.lzy.okgo.callback.StringCallback; +import com.lzy.okgo.model.Response; import org.json.JSONException; import org.json.JSONObject; import java.io.File; +import java.net.URI; import java.util.List; import java.util.SortedMap; import java.util.TreeMap; @@ -49,7 +67,6 @@ public class MyApplication extends Application { OkGo.getInstance().init(this); // NetworkUtils.registerNetworkStatusChangedListener(this); Aria.init(this); - Aria.download(this).register(); Aria.download(this).resumeAllTask(); } @@ -80,7 +97,7 @@ public class MyApplication extends Application { public static String getTaskPackname() { String currentApp = "CurrentNULL"; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { - UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats"); + @SuppressLint("WrongConstant") UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats"); long time = System.currentTimeMillis(); List appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time); if (appList != null && appList.size() > 0) { @@ -139,6 +156,7 @@ public class MyApplication extends Application { private static final String JIGUANG_APP_LOCKEDSTATE = "8"; private static final String JIGUANG_FORCE_INSTALLAPK = "9"; private static final String JIGUANG_FORCE_UNINSTALLAPK = "10"; + private static final String JIGUANG_BIND_DEVIVES = "11"; synchronized public void manageCustomMessage(CustomMessage customMessage) { @@ -184,6 +202,8 @@ public class MyApplication extends Application { case JIGUANG_FORCE_UNINSTALLAPK: unintallApk(EXTRA); break; + case JIGUANG_BIND_DEVIVES: + bindService(EXTRA); } @@ -320,11 +340,11 @@ public class MyApplication extends Application { String package0 = extra.getString("package0"); String package1 = extra.getString("package1"); if (package0.length() != 0) { - boolean qch_jgy_network_disallow = Settings.System.putString(getContentResolver(), "qch_jgy_network_allow", package0); - LogUtils.e("fht", "setAppNetworkstate::" + qch_jgy_network_disallow + ":" + Settings.System.getString(getContentResolver(), "qch_jgy_network_allow")); + boolean qch_jgy_network_allow = Settings.System.putString(getContentResolver(), "qch_jgy_network_allow", package0); + LogUtils.e("fht", "setAppNetworkstate::" + qch_jgy_network_allow + ":" + Settings.System.getString(getContentResolver(), "qch_jgy_network_allow")); } else { - boolean qch_jgy_network_disallow = Settings.System.putString(getContentResolver(), "qch_jgy_network_allow", "invalid"); - LogUtils.e("fht", "setAppNetworkstate::" + qch_jgy_network_disallow + ":" + Settings.System.getString(getContentResolver(), "qch_jgy_network_allow")); + boolean qch_jgy_network_allow = Settings.System.putString(getContentResolver(), "qch_jgy_network_allow", "invalid"); + LogUtils.e("fht", "setAppNetworkstate::" + qch_jgy_network_allow + ":" + Settings.System.getString(getContentResolver(), "qch_jgy_network_allow")); } if (package1.length() != 0) { boolean qch_jgy_network_disallow = Settings.System.putString(getContentResolver(), "qch_jgy_network_disallow", package1); @@ -369,11 +389,21 @@ public class MyApplication extends Application { //静默安装应用,使用okgo,断网会出现问题,等待修改使用aria synchronized private void intallApk(String jsondata) { + try { JSONObject extra = new JSONObject(jsondata); final String packages = extra.getString("package"); ToastUtil.debugShow("收到应用安装消息:包名" + packages); String url = extra.getString("url"); + + if (Aria.download(this).taskExists(url)) { + + List entity = Aria.download(this).getDownloadEntity(url); + for (DownloadEntity downloadEntity : entity) { + Aria.download(this).load(downloadEntity.getId()).cancel(true); + } + } + File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/apk"); file.mkdirs(); Aria.download(this) @@ -402,7 +432,7 @@ public class MyApplication extends Application { // LogUtils.e("downloadProgress", "已下载:" + progress.currentSize + ",总大小:" + progress.totalSize + ",进度:" + progress.fraction + ",当前网速:" + progress.speed); // } // }); - + Aria.download(this).resumeAllTask(); } catch (JSONException e) { e.printStackTrace(); LogUtils.e("intallApk", e.getMessage()); @@ -430,6 +460,97 @@ public class MyApplication extends Application { } } + synchronized void bindService(String json) { + ToastUtil.debugShow("收到绑定设备请求"); + + try { + JSONObject object = new JSONObject(json); + String userName = object.getString("member_name"); + final String id = object.getString("id"); + String phoneNum = object.getString("member_phone"); + final CustomDialog dialog = new CustomDialog(getAppContext()); + dialog.setMessage(phoneNum + "的用户请求绑定你的平板") + .setTitle("绑定请求") + .setPositive("允许") + .setNegtive("拒绝") +// .setSingle(true) + .setOnClickBottomListener(new CustomDialog.OnClickBottomListener() { + @Override + public void onPositiveClick() { + bind(id); + dialog.dismiss(); + } + + @Override + public void onNegtiveClick() { + ToastUtil.show("设备取消绑定"); + dialog.dismiss(); + } + }).show(); +// AlertDialog.Builder builder = new AlertDialog.Builder(this) +// .setTitle("设备绑定") +// .setIcon(R.mipmap.ic_launcher) +// .setCancelable(false) +// .setMessage("用户:“" + userName + "”" + "\n手机:“" + phoneNum + "”" + "\n请求绑定此设备") +// .setPositiveButton("确认绑定", new DialogInterface.OnClickListener() { +// @Override +// public void onClick(DialogInterface dialogInterface, int i) { +// bind(id); +// } +// }) +// .setNegativeButton("取消", new DialogInterface.OnClickListener() { +// @Override +// public void onClick(DialogInterface dialog, int which) { +// dialog.dismiss(); +// ToastUtil.show("设备取消绑定"); +// } +// }); +// AlertDialog ad = builder.create(); +// ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); +// ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失 +// ad.show(); + + } catch (JSONException e) { + e.printStackTrace(); + LogUtils.e("bindService", e.getMessage()); + } + + + } + + synchronized private void bind(String id) { + OkGo.post(UrlPath.BIND_DEVICES) + .params("id", id) + .params("sn", Utils.getSerial()) + .execute(new StringCallback() { + @Override + public void onSuccess(Response response) { + String s = response.body(); + try { + JSONObject jsonObject = new JSONObject(s); + int code = jsonObject.getInt("code"); + String msg = jsonObject.getString("msg"); + if (code == 200) { + ToastUtil.show("绑定成功"); + } else { + ToastUtil.show(msg); + } + + } catch (JSONException e) { + e.printStackTrace(); + } + Intent intent = new Intent(MainActivity.REFRESHACTION); + sendBroadcast(intent); + } + + @Override + public void onError(Response response) { + super.onError(response); + Log.e("bind", response.getException().getMessage()); + } + + }); + } // @Override // public void onDisconnected() { // LogUtils.e("onDisconnected", "网络断开"); @@ -443,15 +564,4 @@ public class MyApplication extends Application { // } - //在这里处理任务执行中的状态,如进度进度条的刷新 - @Download.onTaskRunning - protected void running(DownloadTask task) { - Log.e("aria running", task.getState() + "--" + task.getPercent() + "--" + task.getExtendField()); - } - - @Download.onTaskComplete - void taskComplete(DownloadTask task) { - //在这里处理任务完成的状态 - ApkUtils.installApkInSilence(task.getFilePath(), task.getExtendField()); - } } diff --git a/app/src/main/java/com/info/sn/service/JWebSocketClient.java b/app/src/main/java/com/info/sn/network/JWebSocketClient.java similarity index 96% rename from app/src/main/java/com/info/sn/service/JWebSocketClient.java rename to app/src/main/java/com/info/sn/network/JWebSocketClient.java index fc8511e..522d2d5 100644 --- a/app/src/main/java/com/info/sn/service/JWebSocketClient.java +++ b/app/src/main/java/com/info/sn/network/JWebSocketClient.java @@ -1,4 +1,4 @@ -package com.info.sn.service; +package com.info.sn.network; import android.util.Log; diff --git a/app/src/main/java/com/info/sn/network/UrlPath.java b/app/src/main/java/com/info/sn/network/UrlPath.java index 75fb9a5..b6b851e 100644 --- a/app/src/main/java/com/info/sn/network/UrlPath.java +++ b/app/src/main/java/com/info/sn/network/UrlPath.java @@ -19,6 +19,9 @@ public class UrlPath { //获取当前最顶层应用和电量 public final static String SEND_DRIVE_STATE = HOMEPATHRUL + "Online/online"; - public final static String GET_ALL_PACKAGENAME = HOMEPATHRUL + "/App/allPackage"; + public final static String GET_ALL_PACKAGENAME = HOMEPATHRUL + "App/allPackage"; //获取所有应用包名 + public final static String BIND_DEVICES = HOMEPATHRUL + "Member/binding"; + //绑定设备消息 + } diff --git a/app/src/main/java/com/info/sn/service/APKinstallReceiver.java b/app/src/main/java/com/info/sn/receiver/APKinstallReceiver.java similarity index 95% rename from app/src/main/java/com/info/sn/service/APKinstallReceiver.java rename to app/src/main/java/com/info/sn/receiver/APKinstallReceiver.java index 9568ef8..fa2ffea 100644 --- a/app/src/main/java/com/info/sn/service/APKinstallReceiver.java +++ b/app/src/main/java/com/info/sn/receiver/APKinstallReceiver.java @@ -1,4 +1,4 @@ -package com.info.sn.service; +package com.info.sn.receiver; import android.content.BroadcastReceiver; import android.content.Context; diff --git a/app/src/main/java/com/info/sn/service/BootReceiver.java b/app/src/main/java/com/info/sn/receiver/BootReceiver.java similarity index 91% rename from app/src/main/java/com/info/sn/service/BootReceiver.java rename to app/src/main/java/com/info/sn/receiver/BootReceiver.java index c58b907..2cd6107 100644 --- a/app/src/main/java/com/info/sn/service/BootReceiver.java +++ b/app/src/main/java/com/info/sn/receiver/BootReceiver.java @@ -1,10 +1,14 @@ -package com.info.sn.service; +package com.info.sn.receiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; +import com.info.sn.service.GuardService; +import com.info.sn.service.MyDownloadService; +import com.info.sn.service.StepService; + public class BootReceiver extends BroadcastReceiver { diff --git a/app/src/main/java/com/info/sn/service/MyDownloadService.java b/app/src/main/java/com/info/sn/service/MyDownloadService.java index 8ff0a8f..1235bea 100644 --- a/app/src/main/java/com/info/sn/service/MyDownloadService.java +++ b/app/src/main/java/com/info/sn/service/MyDownloadService.java @@ -18,6 +18,11 @@ import android.view.WindowManager; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import com.arialyy.annotations.Download; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.task.DownloadTask; +import com.blankj.utilcode.util.NetworkUtils; +import com.blankj.utilcode.util.ToastUtils; import com.info.sn.BuildConfig; import com.info.sn.R; import com.info.sn.network.api.HTTPInterface; @@ -28,9 +33,10 @@ import com.lzy.okgo.model.Progress; import com.lzy.okgo.model.Response; import java.io.File; +import java.lang.ref.WeakReference; // 下载管理服务 -public class MyDownloadService extends Service { +public class MyDownloadService extends Service implements NetworkUtils.OnNetworkStatusChangedListener { @Override @@ -46,6 +52,10 @@ public class MyDownloadService extends Service { CheckUpdateByPackageName("com.uiuios.updatetools", handler); //静默升级更新工具 HTTPInterface.getAllAppPackageName(handler); + Aria.download(this).register(); + + Aria.download(this).resumeAllTask(); + //恢复所有未完成的下载任务 return super.onStartCommand(intent, flags, startId); } @@ -265,4 +275,31 @@ public class MyDownloadService extends Service { } } + @Override + public void onDisconnected() { + + } + + @Override + public void onConnected(NetworkUtils.NetworkType networkType) { + Aria.download(this).resumeAllTask(); + } + + + //在这里处理任务执行中的状态,如进度进度条的刷新 + @Download.onTaskRunning + protected void running(DownloadTask task) { + ToastUtils.showShort("正在下载:" + task.getExtendField() + "-" + task.getPercent() + "%"); + Log.e("aria running", "正在下载:" + task.getState() + "--" + task.getPercent() + "--" + task.getExtendField()); + } + + @Download.onTaskComplete + void taskComplete(DownloadTask task) { + //在这里处理任务完成的状态 + ApkUtils.installApp(MyDownloadService.this, task.getFilePath()); + Log.e("taskComplete", task.getExtendField()); + Aria.download(this).load(task.getDownloadEntity().getId()).cancel(); + + } + } diff --git a/app/src/main/java/com/info/sn/service/StepService.java b/app/src/main/java/com/info/sn/service/StepService.java index 98f7155..0a345f9 100644 --- a/app/src/main/java/com/info/sn/service/StepService.java +++ b/app/src/main/java/com/info/sn/service/StepService.java @@ -20,6 +20,7 @@ import android.util.Log; import androidx.annotation.Nullable; import com.info.sn.KeepAliveConnection; +import com.info.sn.network.JWebSocketClient; import com.info.sn.utils.LogUtils; import com.info.sn.utils.ServiceAliveUtils; import com.info.sn.utils.Utils; diff --git a/app/src/main/java/com/info/sn/view/CustomDialog.java b/app/src/main/java/com/info/sn/view/CustomDialog.java new file mode 100644 index 0000000..c37dca7 --- /dev/null +++ b/app/src/main/java/com/info/sn/view/CustomDialog.java @@ -0,0 +1,241 @@ +package com.info.sn.view; + + +import android.app.Dialog; +import android.content.Context; +import android.os.Bundle; +import android.text.TextUtils; +import android.view.View; +import android.view.WindowManager; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.TextView; + +import com.info.sn.R; + + +/** + * description:自定义dialog + */ + +public class CustomDialog extends Dialog { + /** + * 显示的图片 + */ + private ImageView imageIv; + + /** + * 显示的标题 + */ + private TextView titleTv; + + /** + * 显示的消息 + */ + private TextView messageTv; + + /** + * 确认和取消按钮 + */ + private Button negtiveBn, positiveBn; + + /** + * 按钮之间的分割线 + */ + private View columnLineView; + + public CustomDialog(Context context) { + super(context, R.style.CustomDialog); + } + + /** + * 都是内容数据 + */ + private String message; + private String title; + private String positive, negtive; + private int imageResId = -1; + + /** + * 底部是否只有一个按钮 + */ + private boolean isSingle = false; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.custom_dialog); + //按空白处不能取消动画 + setCanceledOnTouchOutside(false); + getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); + //初始化界面控件 + initView(); + //初始化界面数据 + refreshView(); + //初始化界面控件的事件 + initEvent(); + } + + /** + * 初始化界面的确定和取消监听器 + */ + private void initEvent() { + //设置确定按钮被点击后,向外界提供监听 + positiveBn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (onClickBottomListener != null) { + onClickBottomListener.onPositiveClick(); + } + } + }); + //设置取消按钮被点击后,向外界提供监听 + negtiveBn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (onClickBottomListener != null) { + onClickBottomListener.onNegtiveClick(); + } + } + }); + } + + /** + * 初始化界面控件的显示数据 + */ + private void refreshView() { + //如果用户自定了title和message + if (!TextUtils.isEmpty(title)) { + titleTv.setText(title); + titleTv.setVisibility(View.VISIBLE); + } else { + titleTv.setVisibility(View.GONE); + } + if (!TextUtils.isEmpty(message)) { + messageTv.setText(message); + } + //如果设置按钮的文字 + if (!TextUtils.isEmpty(positive)) { + positiveBn.setText(positive); + } else { + positiveBn.setText("确定"); + } + if (!TextUtils.isEmpty(negtive)) { + negtiveBn.setText(negtive); + } else { + negtiveBn.setText("取消"); + } + + if (imageResId != -1) { + imageIv.setImageResource(imageResId); + imageIv.setVisibility(View.VISIBLE); + } else { + imageIv.setVisibility(View.GONE); + } + /** + * 只显示一个按钮的时候隐藏取消按钮,回掉只执行确定的事件 + */ + if (isSingle) { + columnLineView.setVisibility(View.GONE); + negtiveBn.setVisibility(View.GONE); + } else { + negtiveBn.setVisibility(View.VISIBLE); + columnLineView.setVisibility(View.VISIBLE); + } + } + + @Override + public void show() { + super.show(); + refreshView(); + } + + /** + * 初始化界面控件 + */ + private void initView() { + negtiveBn = findViewById(R.id.negtive); + positiveBn = findViewById(R.id.positive); + titleTv = findViewById(R.id.title); + messageTv = findViewById(R.id.message); + imageIv = findViewById(R.id.image); + columnLineView = findViewById(R.id.column_line); + } + + /** + * 设置确定取消按钮的回调 + */ + public OnClickBottomListener onClickBottomListener; + + public CustomDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) { + this.onClickBottomListener = onClickBottomListener; + return this; + } + + public interface OnClickBottomListener { + /** + * 点击确定按钮事件 + */ + void onPositiveClick(); + + /** + * 点击取消按钮事件 + */ + void onNegtiveClick(); + } + + public String getMessage() { + return message; + } + + public CustomDialog setMessage(String message) { + this.message = message; + return this; + } + + public String getTitle() { + return title; + } + + public CustomDialog setTitle(String title) { + this.title = title; + return this; + } + + public String getPositive() { + return positive; + } + + public CustomDialog setPositive(String positive) { + this.positive = positive; + return this; + } + + public String getNegtive() { + return negtive; + } + + public CustomDialog setNegtive(String negtive) { + this.negtive = negtive; + return this; + } + + public int getImageResId() { + return imageResId; + } + + public boolean isSingle() { + return isSingle; + } + + public CustomDialog setSingle(boolean single) { + isSingle = single; + return this; + } + + public CustomDialog setImageResId(int imageResId) { + this.imageResId = imageResId; + return this; + } + +} diff --git a/app/src/main/java/com/info/sn/view/InfoDialog.java b/app/src/main/java/com/info/sn/view/InfoDialog.java new file mode 100644 index 0000000..35dcaca --- /dev/null +++ b/app/src/main/java/com/info/sn/view/InfoDialog.java @@ -0,0 +1,109 @@ +package com.info.sn.view; + +import android.app.Dialog; +import android.content.Context; +import android.graphics.Bitmap; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; + +import com.info.sn.R; + + +public class InfoDialog extends Dialog { + + private InfoDialog(Context context, int themeResId) { + super(context, themeResId); + } + + public static class Builder { + + private View mLayout; + + private ImageView mIcon; + private TextView mTitle; + private TextView mMessage; + private Button mButton; + + private View.OnClickListener mButtonClickListener; + + private InfoDialog mDialog; + + public Builder(Context context) { + mDialog = new InfoDialog(context, R.style.Theme_AppCompat_Dialog); + LayoutInflater inflater = + (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + //加载布局文件 + mLayout = inflater.inflate(R.layout.dialog, null, false); + //添加布局文件到 Dialog + mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT)); + + mIcon = mLayout.findViewById(R.id.dialog_icon); + mTitle = mLayout.findViewById(R.id.dialog_title); + mMessage = mLayout.findViewById(R.id.dialog_message); + mButton = mLayout.findViewById(R.id.dialog_button); + } + + /** + * 通过 ID 设置 Dialog 图标 + */ + public Builder setIcon(int resId) { + mIcon.setImageResource(resId); + return this; + } + + /** + * 用 Bitmap 作为 Dialog 图标 + */ + public Builder setIcon(Bitmap bitmap) { + mIcon.setImageBitmap(bitmap); + return this; + } + + /** + * 设置 Dialog 标题 + */ + public Builder setTitle(@NonNull String title) { + mTitle.setText(title); + mTitle.setVisibility(View.VISIBLE); + return this; + } + + /** + * 设置 Message + */ + public Builder setMessage(@NonNull String message) { + mMessage.setText(message); + return this; + } + + /** + * 设置按钮文字和监听 + */ + public Builder setButton(@NonNull String text, View.OnClickListener listener) { + mButton.setText(text); + mButtonClickListener = listener; + return this; + } + + public InfoDialog create() { + mButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + mDialog.dismiss(); + mButtonClickListener.onClick(view); + } + }); + mDialog.setContentView(mLayout); + mDialog.setCancelable(true); //用户可以点击后退键关闭 Dialog + mDialog.setCanceledOnTouchOutside(false); //用户不可以点击外部来关闭 Dialog + return mDialog; + } + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_dialog.xml b/app/src/main/res/drawable/bg_dialog.xml new file mode 100644 index 0000000..470ebb3 --- /dev/null +++ b/app/src/main/res/drawable/bg_dialog.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/app/src/main/res/drawable/bind.png b/app/src/main/res/drawable/bind.png new file mode 100644 index 0000000000000000000000000000000000000000..0d997ace473f844ded89d316a79147cb0d9ff0bb GIT binary patch literal 3065 zcmaJ@c|6nqAD@^jxy#Lrg^C@B4Kdr)n)`@WWVT^xhi%MJSm=xB5`Ow3xuaCdRmw_< zB#|6RMs!Fhxzh2QzSZyd$FJY#@%Vh+$K&~YzOLsVpHz3(?dxPUWdQ)dI{Xf-r)afW zeWfKu--@9gC()w8#rbi)n892giA@EdDa;@$2u~-4P(7(6N_2QT)fNB{*Qa^=as6DJ zQDi3FjI@d|i=wkcYyiO4K8i&mhf%qpAZiGWVF#YM(F_LBD0X09OBa|63quX1?TBGh zy<%Lw$uVJMBn5204P+aI5(&_$ToNdXet^M2McILW=%PgX)oUmi^aH{TvjhK=l%I<` z2*YGkL6&Bgreql00%V0WgImHatl?Wg2pHTP3Nweo%}wD56by-iTY`Q*U{N$SB^c$2 zb@~}gbg~17a=9!N6w2fA%y{NzOm+wqjzl7%Fa#8VFcl$8InfL*Daw?=G5Do`rE&d)Z98U~Fbv7m4>*lJ0?fG#foA4;eHMsv8H z)c@rBKZQBo(JU&|lgeR6u*ssq1skk}Vxcf>Dv8Tvdo!5_es$43l*wgsLYXWO1_Lto zr79>?Sd+{FcjXK=VA2APV-+JQv^W;7ZFg@apKT43QAYYPhtI2?yYTUc43ov>&) z#?bI6DV6d6lp(;}%9CpMD~`cW~8_UByCf7JWQ zrTjS;$3JqRqGX_}bN#P5|GE;HXLbAAwW7ms-%}YPv$I96j`cBA1puTOcr4mG>e)jg zEnO9-I$d3$A&<0m*oy{_DFD0Yoz?_Y6l$k=ak#tty*n}{0;|J_)6R00($A7PDR>m z&GCfSe5&)*NaT(ZDVVl8FkSpQHtJQdVw0sVa%j3mXIcdzl?|91O!q}7UE%QGDjDun z-m9LGM=#GE7!_M*CRGL)VZL6*^(|`#Zm>cPq|?@iQX<@SQGA z67KPF(z8jriSE~>GC8jYKP9N;%RB^*%h#RQCyp72^?-N|+uSj<+ViqjG509wb57)# z@~jk#m>>Z?!unj*KDaeC?!v3vji3eZJCAhQ`d}uvuV^-#cY5tKX(Y4@jCDUtHo!^c zZB@~K<)oP_*PiYG&WG40UAvK!N9RSz_P&+0XfV}pBo-Peyg1MaYQ--i0Lt>r6D<)TBYZg+<|hpu&JyJGnOnjXVul;l6tbqfq{&5nK*x@Jyqa{jv{Q4OGpo;h?)1BR(Iri? z^6;y?eHYWZtgdCMPt>+TS`&u}UOa={9riafhtWP&{^UtW{}BY)`@NOxHuA*szMein zynWJW*W7%0eHlaNXd zQzvHU42M+#jJ`j%Lit_XRp5xXt6PtrQhM(<`qQjkfA1G z;wST>ibzdFPo<;R?tGgFx(06}R!~`*bf+}3!+gqr6ysP^`FV}TS6lfV&OKxqjF)9kO&ywzPr9_17@s(PA0h_+P*irMy>qb9U;JQ)CxxFZ&=4d$ z6yJGXH47j_>9WGT*-IJzO%T{QWtwLsI%6$pvG)N~eS3mO* z(kv9-lsqn&7lhrqx~5{NSg;{^JKywVR1I<^u$AB{p;#dSkI?EIT+W=0Z7Q2S%>8z~ zNb5CnH~oRxtz>b;rl;q;3g+6J?#+pnZp5ng<0C)1jZ-1T7017pbn6itYAGEr2epPf z-u1jl`^)J1yK`PIRO=HF8YY_BsIInJrMah<@UP2bz|yzOKa?FX&5yq+)SC*jBVryc z7Syyw;%Ze6zrXXqr_0q7`=u0{LRz3{ZuDkV(VE82EUpG|G=V{Mg_4|5U zAv$}Zs0FdyUS*KUk=d3z>UH{w0XJRNzNH_rYY2)YYU?`-^VZ69#V>!fUZCB;scUNv zz;nMOpWWY5i|#Mhyy|vxYGyRqw%X#2Mcpy}2>a-b%`BAbTuS8Q@$SVGgWbA$%(thF zGn)hH?(cWhFKAvx%ASQHiu|(>W5eSo!YA7lf1GKwGy!3TI$P3`u%6)M%cIpyZ(f?W^G#% z7#-_?PKe5k@p@I+_sygRE6LP>eYfuiW + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 0eb88fe..1eb2ea0 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -7,5 +7,17 @@ @color/colorPrimaryDark @color/colorAccent +