-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBlurActivity.java
executable file
·161 lines (139 loc) · 5.26 KB
/
BlurActivity.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.background;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;
import androidx.work.Data;
import androidx.work.WorkInfo;
import com.bumptech.glide.Glide;
import static com.example.background.Constants.KEY_IMAGE_URI;
public class BlurActivity extends AppCompatActivity {
private static final String TAG = BlurActivity.class.getSimpleName();
private BlurViewModel mViewModel;
private ImageView mImageView;
private ProgressBar mProgressBar;
private Button mGoButton, mOutputButton, mCancelButton;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blur);
// Get the ViewModel
mViewModel = ViewModelProviders.of(this).get(BlurViewModel.class);
// Get all of the Views
mImageView = findViewById(R.id.image_view);
mProgressBar = findViewById(R.id.progress_bar);
mGoButton = findViewById(R.id.go_button);
mOutputButton = findViewById(R.id.see_file_button);
mCancelButton = findViewById(R.id.cancel_button);
// Image uri should be stored in the ViewModel; put it there then display
Intent intent = getIntent();
String imageUriExtra = intent.getStringExtra(KEY_IMAGE_URI);
mViewModel.setImageUri(imageUriExtra);
if (mViewModel.getImageUri() != null) {
Glide.with(this).load(mViewModel.getImageUri()).into(mImageView);
}
// Setup blur image file button
mGoButton.setOnClickListener(view -> mViewModel.applyBlur(getBlurLevel()));
mViewModel.getSavedWorkInfo().observe(this, listOfWorkInfos -> {
// If there are no matching work info, do nothing
if (listOfWorkInfos == null || listOfWorkInfos.isEmpty()) {
return;
}
// We only care about the first output status.
// Every continuation has only one worker tagged TAG_OUTPUT
WorkInfo workInfo = listOfWorkInfos.get(0);
boolean finished = workInfo.getState().isFinished();
if (!finished) {
showWorkInProgress();
} else {
showWorkFinished();
Data outputData = workInfo.getOutputData();
String outputImageUri = outputData.getString(KEY_IMAGE_URI);
Log.d(TAG, "outputImageUri = " + outputImageUri);
// If there is an output file show "See File" button
if (!TextUtils.isEmpty(outputImageUri)) {
Log.d(TAG, "Setting uri on viewmodel");
mViewModel.setOutputUri(outputImageUri);
mOutputButton.setVisibility(View.VISIBLE);
} else {
Log.d(TAG, "Got empty uri");
Toast.makeText(this, "Got empty Uri", Toast.LENGTH_SHORT).show();
}
}
});
mOutputButton.setOnClickListener(view -> {
Uri currentUri = mViewModel.getOutputUri();
if (currentUri != null) {
Intent actionView = new Intent(Intent.ACTION_VIEW, currentUri);
if (actionView.resolveActivity(getPackageManager()) != null) {
startActivity(actionView);
} else {
Toast.makeText(this, "Failed to resolve Activity", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Current Uri is null", Toast.LENGTH_SHORT).show();
}
});
// Hookup the Cancel button
mCancelButton.setOnClickListener(view -> mViewModel.cancelWork());
}
/**
* Shows and hides views for when the Activity is processing an image
*/
private void showWorkInProgress() {
mProgressBar.setVisibility(View.VISIBLE);
mCancelButton.setVisibility(View.VISIBLE);
mGoButton.setVisibility(View.GONE);
mOutputButton.setVisibility(View.GONE);
}
/**
* Shows and hides views for when the Activity is done processing an image
*/
private void showWorkFinished() {
mProgressBar.setVisibility(View.GONE);
mCancelButton.setVisibility(View.GONE);
mGoButton.setVisibility(View.VISIBLE);
}
/**
* Get the blur level from the radio button as an integer
*
* @return Integer representing the amount of times to blur the image
*/
private int getBlurLevel() {
RadioGroup radioGroup = findViewById(R.id.radio_blur_group);
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.radio_blur_lv_1:
return 1;
case R.id.radio_blur_lv_2:
return 2;
case R.id.radio_blur_lv_3:
return 3;
}
return 1;
}
}