Commit 02077da9 authored by DESKTOP-NFGF3PG\zxa01's avatar DESKTOP-NFGF3PG\zxa01

update download file

parent 738ed6f5
package com.example.zxa01.iotclient.common.pojo.index;
import android.provider.DocumentsContract;
import com.example.zxa01.iotclient.common.pojo.device.Device;
import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyPolicy;
import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyPolicyReport;
import java.util.HashMap;
import java.util.List;
public class PrivacyPolicyReportResponse extends PrivacyPolicyReport {
private HashMap<Integer, Boolean> choices ;
public PrivacyPolicyReportResponse(){
super();
}
public PrivacyPolicyReportResponse(String id, String version, String description, Device device, List<PrivacyPolicy> policies, DocumentsContract.Document document, HashMap<Integer, Boolean> choices) {
super();
this.choices = choices;
}
public HashMap<Integer, Boolean> getChoices() {
return choices;
}
public void setChoices(HashMap<Integer, Boolean> choices) {
this.choices = choices;
}
}
......@@ -10,9 +10,12 @@ public class DefaultData {
private static DefaultData defaultData = new DefaultData();
private LoginMessage loginMessage;
private List<String> devices = new LinkedList<>();
public String gatewayServer = "http://192.168.2.90:8080";
public String cloudServer = "http://192.168.2.90:8081";
private DefaultData() {
loginMessage = new LoginMessage("192.168.2.69:8080", "user", "1234");
loginMessage = new LoginMessage(gatewayServer, "user", "1234");
devices.add("a1252c49-4188-4e6d-a32e-66604c664fb8");
devices.add("abe5ca7b-780e-4857-87e6-014870fe0a3e");
}
......
......@@ -2,6 +2,7 @@ package com.example.zxa01.iotclient.common.tools;
import com.example.zxa01.iotclient.common.pojo.device.Device;
import com.example.zxa01.iotclient.common.pojo.index.PrivacyChoiceResponse;
import com.example.zxa01.iotclient.common.pojo.index.PrivacyPolicyReportResponse;
import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyChoice;
import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyPolicyReport;
import com.example.zxa01.iotclient.common.singleton.Config;
......@@ -26,7 +27,7 @@ public class Api {
public static ApiInterface getApi() {
retrofit = new Retrofit.Builder()
.baseUrl("http://" + Config.getConfig().getGateway())
.baseUrl(Config.getConfig().getGateway())
.client(new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.cache(null)
......@@ -54,8 +55,8 @@ public class Api {
Call<Device> bindDeviceAndGateway(@Path("udn") String udn);
// 取得該裝置的隱私政策與相關資料
@GET("/privacy/{udn}")
Call<PrivacyPolicyReport> readPrivacyPolicyReportByDevice(@Path("udn") String udn);
@GET("/privacy/{udn}/{account}")
Call<PrivacyPolicyReportResponse> readPrivacyPolicyReportByDevice(@Path("udn") String udn, @Path("account")String account);
// 表達隱私偏好
@POST("/choice")
......
......@@ -93,25 +93,19 @@ public class DownloadFile {
connection.connect();
downloadFileName = java.net.URLDecoder.decode(
connection.getHeaderField("Content-Disposition").replace("Attachment; filename=",""),
connection.getHeaderField("Content-Disposition")
.substring(connection.getHeaderField("Content-Disposition").indexOf("=")+1)
.replace("UTF-8''","")
.trim(),
String.valueOf(StandardCharsets.UTF_8));
// check SDCard
if (new CheckForSDCard().isSDCardPresent()) {
apkStorage = new File(
Environment.getExternalStorageDirectory().toString() + "/report");
} else{
if (!new CheckForSDCard().isSDCardPresent()) {
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);
outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), downloadFileName);
if (!outputFile.exists()) {
outputFile.createNewFile();
outputFile.setWritable(true);
......
......@@ -5,6 +5,8 @@ import android.databinding.BaseObservable;
import android.support.annotation.NonNull;
import android.util.Log;
import com.example.zxa01.iotclient.common.pojo.index.PrivacyPolicyReportResponse;
import com.example.zxa01.iotclient.common.singleton.Config;
import com.example.zxa01.iotclient.common.tools.Api;
import com.example.zxa01.iotclient.common.pojo.device.Device;
import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyPolicyReport;
......@@ -29,16 +31,16 @@ public class DetailModel extends BaseObservable {
}
public void readDevice(@NonNull String udn) {
Api.getApi().readPrivacyPolicyReportByDevice(udn).enqueue(new Callback<PrivacyPolicyReport>() {
Api.getApi().readPrivacyPolicyReportByDevice(udn, Config.getConfig().getUser().getAccount()).enqueue(new Callback<PrivacyPolicyReportResponse>() {
@Override
public void onResponse(Call<PrivacyPolicyReport> call, Response<PrivacyPolicyReport> response) {
public void onResponse(Call<PrivacyPolicyReportResponse> call, Response<PrivacyPolicyReportResponse> response) {
Log.i("readDevices - onResponse()", "success");
setDeviceMLD(response == null || response.body() == null ?
null : response.body().getDevice());
}
@Override
public void onFailure(Call<PrivacyPolicyReport> call, Throwable t) {
public void onFailure(Call<PrivacyPolicyReportResponse> call, Throwable t) {
Log.e("readDevice - onFailure()", t.getMessage(), t);
}
});
......
package com.example.zxa01.iotclient.component.detail;
import com.example.zxa01.iotclient.common.pojo.device.Device;
import com.example.zxa01.iotclient.common.singleton.Config;
import com.example.zxa01.iotclient.common.singleton.DefaultData;
import com.example.zxa01.iotclient.common.tools.DownloadFile;
import com.example.zxa01.iotclient.component.privacy.PrivacyActivity;
import android.arch.lifecycle.MutableLiveData;
......@@ -48,8 +50,6 @@ public class DetailViewModel extends ViewModel {
}
public void downloadPrivacyReport() {
// fake file just for test
String url = "https://www.ey.gov.tw/File/4EBD1D15E6556A04?A=C";
new DownloadFile(context, url);
new DownloadFile(context, DefaultData.getDefaultData().cloudServer+"/document/"+device.get().getUdn());
}
}
......@@ -75,7 +75,7 @@ public class HomeActivity extends AppCompatActivity {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK) {
new DeviceBindViewModel().bindDeviceAndGateway(intent.getStringExtra("SCAN_RESULT"));
new DeviceBindViewModel(this).bindDeviceAndGateway(intent.getStringExtra("SCAN_RESULT"));
} else {
new AlertDialog.Builder(this)
.setMessage(R.string.bind_qrcode_error)
......
......@@ -35,7 +35,7 @@ public class DeviceFragment extends Fragment {
viewModel = new DeviceViewModel(binding.getRoot().getContext());
binding.setViewModel(viewModel);
binding.fab.setOnClickListener(item -> drawDialog());
binding.fabRefresh.setOnClickListener(item -> init());
binding.fabRefresh.setOnClickListener(item -> viewModel.readDevices());
binding.deviceRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
deviceBindFragment = new DeviceBindFragment();
}
......
......@@ -14,8 +14,8 @@ import com.example.zxa01.iotclient.databinding.FragmentDeviceBindBinding;
public class DeviceBindFragment extends DialogFragment {
private DeviceBindViewModel viewModel;
private FragmentDeviceBindBinding binding;
public DeviceBindViewModel viewModel;
public FragmentDeviceBindBinding binding;
public DeviceBindFragment() {
}
......
......@@ -13,6 +13,7 @@ import android.support.v7.app.AlertDialog;
import com.example.zxa01.iotclient.R;
import com.example.zxa01.iotclient.common.singleton.DefaultData;
import com.example.zxa01.iotclient.component.home.HomeActivity;
public class DeviceBindViewModel extends ViewModel {
......@@ -25,15 +26,19 @@ public class DeviceBindViewModel extends ViewModel {
public ObservableField<String> udn = new ObservableField<>();
private DeviceBindModel deviceBindModel = new DeviceBindModel();
private Fragment fragment;
private Context context;
private AlertDialog dialog;
public DeviceBindViewModel(){
public DeviceBindViewModel(Context context){
this.context = context;
}
public DeviceBindViewModel(Fragment fragment) {
this.fragment = fragment;
this.context = fragment.getContext();
drawDialog();
// default udn
udn.set(DefaultData.getDefaultData().getDevices().get(0));
}
/**
......@@ -42,11 +47,9 @@ public class DeviceBindViewModel extends ViewModel {
public void bindDeviceAndGateway(String udn) {
deviceBindModel.bindDeviceAndGateway(udn);
context.startActivity(new Intent(context,HomeActivity.class));
}
public void bindDeviceAndGateway() {
deviceBindModel.bindDeviceAndGateway(udn.get());
}
public void qrcode() {
try {
......
......@@ -32,6 +32,7 @@ public class PrivacyActivity extends AppCompatActivity {
viewModel.fetchPrivacyPolicyReportByDevice(getIntent().getStringExtra("udn"));
viewModel.observePrivacyPolicyReportMLD().observe(this, viewModel::setPrivacyPolicyReport);
viewModel.observeIsLoadingMLD().observe(this, viewModel::setIsUpload);
// viewModel.observeMappingChoiceMLD().observe(this,viewModel::);
}
@Override
......
......@@ -6,6 +6,7 @@ import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.Log;
import com.example.zxa01.iotclient.common.pojo.index.PrivacyPolicyReportResponse;
import com.example.zxa01.iotclient.common.tools.Api;
import com.example.zxa01.iotclient.common.pojo.index.PrivacyChoiceResponse;
import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyChoice;
......@@ -13,22 +14,26 @@ import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyContent;
import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyPolicyReport;
import com.example.zxa01.iotclient.common.singleton.Config;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class PrivacyModel extends BaseObservable {
private MutableLiveData<PrivacyPolicyReportResponse> privacyPolicyReportMLD = new MutableLiveData<>();
private MutableLiveData<Boolean> isUploadMLD = new MutableLiveData<>();
private MutableLiveData<PrivacyPolicyReport> privacyPolicyReportMLD = new MutableLiveData<>();
public PrivacyModel() {
privacyPolicyReportMLD.setValue(new PrivacyPolicyReport());
privacyPolicyReportMLD.setValue(new PrivacyPolicyReportResponse());
isUploadMLD.setValue(false);
}
public MutableLiveData<PrivacyPolicyReport> getPrivacyPolicyReportMLD() {
public MutableLiveData<PrivacyPolicyReportResponse> getPrivacyPolicyReportMLD() {
return privacyPolicyReportMLD;
}
......@@ -37,20 +42,19 @@ public class PrivacyModel extends BaseObservable {
}
public void readPrivacyPolicyReportByDevice(@NonNull String udn) {
Api.getApi().readPrivacyPolicyReportByDevice(udn).enqueue(new Callback<PrivacyPolicyReport>() {
Api.getApi().readPrivacyPolicyReportByDevice(udn,Config.getConfig().getUser().getAccount()).enqueue(new Callback<PrivacyPolicyReportResponse>() {
@Override
public void onResponse(Call<PrivacyPolicyReport> call, Response<PrivacyPolicyReport> response) {
public void onResponse(Call<PrivacyPolicyReportResponse> call, Response<PrivacyPolicyReportResponse> response) {
privacyPolicyReportMLD.setValue(response.body());
}
@Override
public void onFailure(Call<PrivacyPolicyReport> call, Throwable t) {
public void onFailure(Call<PrivacyPolicyReportResponse> call, Throwable t) {
Log.e("readPrivacyPolicyReportByDevice - onFailure()", t.getMessage(), t);
}
});
}
public void setPrivacyChoice(@NonNull PrivacyContent privacyContent, @NonNull boolean isAccepted) {
isUploadMLD.setValue(true);
Api.getApi().setPrivacyChoice(new PrivacyChoice()
......
package com.example.zxa01.iotclient.component.privacy;
import com.example.zxa01.iotclient.R;
import com.example.zxa01.iotclient.common.pojo.index.PrivacyPolicyReportResponse;
import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyContent;
import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyPolicy;
import com.example.zxa01.iotclient.common.pojo.privacy.PrivacyPolicyReport;
......@@ -42,10 +43,11 @@ public class PrivacyViewModel extends ViewModel {
privacyModel.readPrivacyPolicyReportByDevice(udn);
}
public MutableLiveData<PrivacyPolicyReport> observePrivacyPolicyReportMLD() {
public MutableLiveData<PrivacyPolicyReportResponse> observePrivacyPolicyReportMLD() {
return privacyModel.getPrivacyPolicyReportMLD();
}
public void setPrivacyPolicyReport(PrivacyPolicyReport privacyPolicyReport) {
if (privacyPolicyReport != null && privacyPolicyReport.getId() != null) {
this.privacyPolicyReport.set(privacyPolicyReport);
......@@ -65,6 +67,14 @@ public class PrivacyViewModel extends ViewModel {
return null;
}
public boolean getPrivacyResponseAt(@NonNull Integer index) {
if (privacyModel.getPrivacyPolicyReportMLD().getValue() != null &&
privacyModel.getPrivacyPolicyReportMLD().getValue().getPolicies().size() > index) {
return privacyModel.getPrivacyPolicyReportMLD().getValue().getChoices().get(index);
}
return false;
}
public void onSetPrivacyChoice(@NonNull Integer index, View view) {
if (privacyModel.getPrivacyPolicyReportMLD().getValue() != null &&
privacyModel.getPrivacyPolicyReportMLD().getValue().getPolicies().size() > index) {
......
......@@ -75,7 +75,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="@{()->viewModel.bindDeviceAndGateway()}"
android:onClick="@{()->viewModel.bindDeviceAndGateway(viewModel.udn)}"
android:text="@string/bind_button_correct" />
</LinearLayout>
......
......@@ -48,6 +48,7 @@
android:onClick="@{(view)->viewModel.onSetPrivacyChoice(position,view)}"
android:text="@string/privacy_content"
android:textAlignment="textEnd"
android:checked="@{viewModel.getPrivacyResponseAt(position)}"
android:thumbTextPadding="@dimen/padding_lg" />
</LinearLayout>
......
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