Commit f9b3e5d7 authored by DESKTOP-NFGF3PG\zxa01's avatar DESKTOP-NFGF3PG\zxa01

add notification

parent 8cffb858
......@@ -6,6 +6,9 @@
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
......
package com.example.zxa01.iotclient.common.http;
import android.Manifest;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;
import com.example.zxa01.iotclient.R;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import static android.support.v4.app.ActivityCompat.requestPermissions;
public class DownloadTask {
private static final String TAG = "Download File";
public static final String CHANNELID = "118";
public static final CharSequence CHANNELCHAR = "Download Report File";
private NotificationManager notificationManager;
private Context context;
private String downloadUrl;
private String downloadFileName;
private AlertDialog dialog;
public DownloadTask(Context context, String downloadUrl) {
this.context = context;
this.downloadUrl = downloadUrl;
this.dialog = new AlertDialog.Builder(context)
.setMessage(R.string.privacy_loading_message)
.setTitle(R.string.detail_report_download)
.create();
isStoragePermissionGranted();
new DownloadingTask().execute();
}
private class DownloadingTask extends AsyncTask<Void, Void, Void> {
File apkStorage = null;
File outputFile = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.show();
}
@Override
protected void onPostExecute(Void result) {
try {
dialog.hide();
if (outputFile != null) {
notification();
Toast.makeText(context, "Downloaded Successfully", Toast.LENGTH_SHORT).show();
} else {
dialogDelay();
Toast.makeText(context, "Downloaded Failed", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
dialogDelay();
Toast.makeText(context, "Downloaded Failed with Exception", Toast.LENGTH_SHORT).show();
}
super.onPostExecute(result);
}
@Override
protected Void doInBackground(Void... arg0) {
try {
URL url = new URL(downloadUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
downloadFileName = java.net.URLDecoder.decode(
connection.getHeaderField("Content-Disposition").replace("Attachment; filename=",""),
String.valueOf(StandardCharsets.UTF_8));
// check SDCard
if (new CheckForSDCard().isSDCardPresent()) {
apkStorage = new File(
Environment.getExternalStorageDirectory().toString() + "/report");
} else{
Toast.makeText(context, "There is no SD Card.", Toast.LENGTH_SHORT).show();
}
// check directory
if (!apkStorage.exists()) {
apkStorage.mkdir();
Log.e(TAG, "Directory Created.");
}
// check file
outputFile = new File(apkStorage, downloadFileName);
if (!outputFile.exists()) {
outputFile.createNewFile();
outputFile.setWritable(true);
Log.e(TAG, "File Created");
}
// output handle
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream input = new BufferedInputStream(url.openStream(),
8192);
OutputStream output = new FileOutputStream(outputFile.getPath());
byte[] data = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
fos.flush();
fos.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
outputFile = null;
Log.e(TAG, "Download Error Exception " + e.getMessage());
}
return null;
}
}
class CheckForSDCard {
public boolean isSDCardPresent() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return true;
}
return false;
}
}
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (context.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
requestPermissions((Activity)context,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else {
Log.v(TAG,"Permission is granted");
return true;
}
}
private void dialogDelay() {
new Handler().postDelayed(() ->{}, 3000);
}
private void notification(){
int NOTIFY_ID = 0;
NotificationCompat.Builder builder;
if (notificationManager == null) {
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = notificationManager.getNotificationChannel(CHANNELID);
if (mChannel == null) {
mChannel = new NotificationChannel(CHANNELID, CHANNELCHAR, importance);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(mChannel);
}
builder = new NotificationCompat.Builder(context, CHANNELID);
builder.setContentTitle("下載成功")
.setSmallIcon(android.R.drawable.ic_popup_reminder)
.setContentText(downloadFileName)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setTicker("下載成功")
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
}
else {
builder = new NotificationCompat.Builder(context, CHANNELID);
builder.setContentTitle("下載成功")
.setSmallIcon(android.R.drawable.ic_popup_reminder)
.setContentText(downloadFileName)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setTicker("下載成功")
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setPriority(Notification.PRIORITY_HIGH);
}
Notification notification = builder.build();
notificationManager.notify(NOTIFY_ID, notification);
}
}
\ No newline at end of file
package com.example.zxa01.iotclient.common.pojo.index;
import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyContent;
import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyChoice;
public class PrivacyChoiceResponse {
private long id;
private String localDateTime;
private PrivacyContent privacyContent;
private PrivacyChoice privacyChoice;
public PrivacyChoiceResponse(){
......@@ -28,11 +27,11 @@ public class PrivacyChoiceResponse {
this.localDateTime = localDateTime;
}
public PrivacyContent getPrivacyContent() {
return privacyContent;
public PrivacyChoice getPrivacyChoice() {
return privacyChoice;
}
public void setPrivacyContent(PrivacyContent privacyContent) {
this.privacyContent = privacyContent;
public void setPrivacyChoice(PrivacyChoice privacyChoice) {
this.privacyChoice = privacyChoice;
}
}
package com.example.zxa01.iotclient.component.detail;
import com.example.zxa01.iotclient.R;
import com.example.zxa01.iotclient.common.http.DownloadTask;
import com.example.zxa01.iotclient.common.pojo.device.Device;
import com.example.zxa01.iotclient.component.privacy.PrivacyActivity;
import android.arch.lifecycle.MutableLiveData;
......@@ -9,16 +10,28 @@ import android.content.Context;
import android.content.Intent;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
public class DetailViewModel extends ViewModel {
public ObservableBoolean isLoading = new ObservableBoolean(true);
public ObservableField<Device> device = new ObservableField<>();
private DetailModel detailModel = new DetailModel();
private Context context;
private Toast toast;
public DetailViewModel(Context context) {
this.context = context;
......@@ -47,9 +60,8 @@ public class DetailViewModel extends ViewModel {
}
public void downloadPrivacyReport() {
// TODO download
Toast toast = Toast.makeText(context, R.string.detail_report_download, Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 15);
toast.show();
// fake file just for test
String url = "https://www.ey.gov.tw/File/4EBD1D15E6556A04?A=C";
new DownloadTask(context, url);
}
}
......@@ -56,7 +56,7 @@ public class RecordViewModel extends ViewModel {
new Intent(context, PrivacyActivity.class)
.putExtra("udn",
recordModel.getPrivacyChoiceResponsesMLD().getValue().get(index)
.getPrivacyContent().getDevice().getUdn()));
.getPrivacyChoice().getPrivacyContent().getDevice().getUdn()));
}
}
......
......@@ -24,7 +24,7 @@ public class LoginActivity extends AppCompatActivity {
}
private void binding() {
viewModel = new LoginViewModel(binding.getRoot().getContext());
viewModel = new LoginViewModel(this);
binding.setViewModel(viewModel);
binding.linearLoginForm.setOnClickListener(view -> hideKeyboard(LoginActivity.this));
}
......
......@@ -34,7 +34,6 @@ public class LoginModel extends BaseObservable {
return message.getAccount() != null &&
message.getPassword() != null &&
message.getGateway() != null;
}
private void settingConfig(@NonNull LoginMessage message) {
......
......@@ -3,7 +3,6 @@ package com.example.zxa01.iotclient.component.login;
import com.example.zxa01.iotclient.common.shared.DefaultData;
import com.example.zxa01.iotclient.component.home.HomeActivity;
import com.example.zxa01.iotclient.common.pojo.auth.LoginMessage;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import android.content.Context;
......@@ -25,6 +24,7 @@ public class LoginViewModel extends ViewModel {
loginModel.login(loginMessage.get());
}
public void checkAuthorized(Boolean isAuthorized) {
if (isAuthorized) {
context.startActivity(
......
......@@ -54,7 +54,10 @@ public class PrivacyModel extends BaseObservable {
public void setPrivacyChoice(@NonNull PrivacyContent privacyContent, @NonNull boolean isAccepted) {
isUploadMLD.setValue(true);
Api.getApi().setPrivacyChoice(transferPrivacyContent(privacyContent, isAccepted))
Api.getApi().setPrivacyChoice(new Gson().toJson(
new PrivacyChoice().setPrivacyContent(
privacyContent.setUser(Config.getConfig().getUser()))
.setAccepted(isAccepted)))
.enqueue(new Callback<PrivacyChoiceResponse>() {
@Override
public void onResponse(Call<PrivacyChoiceResponse> call, Response<PrivacyChoiceResponse> response) {
......@@ -73,11 +76,4 @@ public class PrivacyModel extends BaseObservable {
new Handler().postDelayed(() -> isUploadMLD.setValue(false), 500);
}
private String transferPrivacyContent(@NonNull PrivacyContent privacyContent, @NonNull boolean isAccepted) {
return new Gson().toJson(
new PrivacyChoice().setPrivacyContent(
privacyContent.setUser(Config.getConfig().getUser()))
.setAccepted(isAccepted));
}
}
......@@ -47,16 +47,6 @@
android:src="@drawable/ic_add_black_24dp"
app:backgroundTint="@color/colorPrimary" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/no_content"
android:textAlignment="center"
android:textSize="@dimen/font"
android:visibility="@{viewModel.isLoading &amp;&amp; viewModel.isNoContent ? View.GONE : View.VISIBLE}" />
</FrameLayout>
</layout>
......@@ -59,16 +59,5 @@
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAlignment="center"
android:textSize="@dimen/font"
android:text="@string/no_content"
android:visibility="@{viewModel.isLoading &amp;&amp; viewModel.isNoContent ? View.GONE : View.VISIBLE}" />
</FrameLayout>
</layout>
\ No newline at end of file
......@@ -58,17 +58,6 @@
app:layout_constraintStart_toEndOf="@+id/image_device"
tools:text="subtitle" />
<TextView
android:id="@+id/text_device_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:textSize="12sp"
android:text="@{viewModel.getDeviceAt(position).status.toString()}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/image_device"
tools:text="connection" />
</android.support.constraint.ConstraintLayout>
</layout>
\ No newline at end of file
......@@ -34,7 +34,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{viewModel.getPrivacyChoiceAt(position).privacyContent.device.name}"
android:text="@{viewModel.getPrivacyChoiceAt(position).privacyChoice.privacyContent.device.name}"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:textStyle="bold"
......@@ -52,7 +52,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{viewModel.getPrivacyChoiceAt(position).privacyContent.policy.description}"
android:text="@{viewModel.getPrivacyChoiceAt(position).privacyChoice.privacyContent.policy.description}"
tools:text="本APP會蒐集使用者周遭溫度作為第三方資料之地區環境分析資料。" />
<LinearLayout
......
......@@ -23,6 +23,7 @@
<string name="detail_preference_setting">隱私選項設定</string>
<string name="detail_detect_report">檢測報告下載</string>
<string name="detail_report_download">下載中...</string>
<string name="detail_report_downloaded">下載成功</string>
<!--Privacy-->
<string name="privacy_report_title">隱私政策報告</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment