This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
/
MainActivity.java
239 lines (202 loc) · 8.88 KB
/
MainActivity.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2016 Google Inc.
//
// 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.google.codelabs.cosu;
import android.Manifest;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends Activity {
private Button takePicButton;
private Button lockTaskButton;
private ImageView imageView;
private String mCurrentPhotoPath;
private int permissionCheck;
private PackageManager mPackageManager;
private DevicePolicyManager mDevicePolicyManager;
private ComponentName mAdminComponentName;
private static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 2;
private static final String FILE_TAG = "File Creation";
public static final String EXTRA_FILEPATH =
"com.google.codelabs.cosu.EXTRA_FILEPATH";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePicButton = (Button) findViewById(R.id.pic_button);
takePicButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
File photoFile=null;
try{
photoFile = createImageFile();
} catch (IOException e) {
Log.e(FILE_TAG,e.getMessage());
}
if (photoFile != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
else{
Toast.makeText(
getApplicationContext(),R.string.no_camera_apps,
Toast.LENGTH_SHORT)
.show();
}
}
});
mDevicePolicyManager = (DevicePolicyManager)
getSystemService(Context.DEVICE_POLICY_SERVICE);
mAdminComponentName = DeviceAdminReceiver.getComponentName(this);
mPackageManager = this.getPackageManager();
lockTaskButton = (Button) findViewById(R.id.start_lock_button);
lockTaskButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ( mDevicePolicyManager.isDeviceOwnerApp(
getApplicationContext().getPackageName())) {
Intent lockIntent = new Intent(getApplicationContext(),
LockedActivity.class);
lockIntent.putExtra(EXTRA_FILEPATH,mCurrentPhotoPath);
mPackageManager.setComponentEnabledSetting(
new ComponentName(getApplicationContext(),
LockedActivity.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
startActivity(lockIntent);
finish();
} else {
Toast.makeText(getApplicationContext(),
R.string.not_lock_whitelisted,Toast.LENGTH_SHORT)
.show();
}
}
});
imageView = (ImageView) findViewById(R.id.main_imageView);
// Check to see if permission to access external storage is granted,
// and request if not
permissionCheck = ContextCompat.checkSelfPermission(
this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String []{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
// Check to see if started by LockActivity and disable LockActivity if so
Intent intent = getIntent();
if(intent.getIntExtra(LockedActivity.LOCK_ACTIVITY_KEY,0) ==
LockedActivity.FROM_LOCK_ACTIVITY){
mDevicePolicyManager.clearPackagePersistentPreferredActivities(
mAdminComponentName,getPackageName());
mPackageManager.setComponentEnabledSetting(
new ComponentName(getApplicationContext(), LockedActivity.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}
private File createImageFile() throws IOException {
//Check for storage permission
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
// Create an image file name
String timeStamp =
new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getApplicationContext().getExternalFilesDir(
null);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
return null;
}
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
setImageToView();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
// If request is cancelled, results array is empty
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
permissionCheck=grantResults[0];
} else {
takePicButton.setEnabled(false);
}
return;
}
}
}
private void setImageToView(){
//Save the file in gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
// Get the dimensions of the view
int targetH = imageView.getMaxHeight();
int targetW = imageView.getMaxWidth();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoH = bmOptions.outHeight;
int photoW = bmOptions.outWidth;
// Determine how much to scale down image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
Bitmap imageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
imageView.setImageBitmap(imageBitmap);
// enable lock task button
lockTaskButton.setEnabled(true);
}
}