Skip to content

Commit 6540da2

Browse files
committed
Merge /ssd/testdpc-qt-release/vendor/unbundled_google/packages/TestDPC into qt-1
2 parents 3f316bf + 04c052c commit 6540da2

File tree

52 files changed

+1989
-265
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1989
-265
lines changed

app/build.gradle

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ apply plugin: 'com.android.application'
33
ext {
44
/* Version code for *next* release, bump *after* a release is created. */
55
// 1 or more digits
6-
versionMajor = 5
6+
versionMajor = 6
77
// exactly 1 digit
88
versionMinor = 0
99
// exactly 2 digits
10-
versionBuild = 04
10+
versionBuild = 00
1111
}
1212

1313
android {
14-
compileSdkVersion 28
14+
compileSdkVersion 'android-Q'
1515
buildToolsVersion "28.0.0"
1616

1717
defaultConfig {
1818
applicationId "com.afwsamples.testdpc"
1919
minSdkVersion 21
20-
targetSdkVersion 28
20+
targetSdkVersion 'Q'
2121
versionCode versionMajor * 1000 + versionMinor * 100 + versionBuild
2222
versionName "${versionMajor}.${versionMinor}.${versionBuild}"
2323
multiDexEnabled true
@@ -43,7 +43,7 @@ android {
4343

4444
lintOptions {
4545
check 'NewApi'
46-
abortOnError true
46+
abortOnError false
4747
xmlReport false
4848
textReport true
4949
textOutput "stdout"
@@ -70,6 +70,53 @@ android {
7070
output.assemble.dependsOn lintTask
7171
}
7272
}
73+
74+
task stripTestOnlyNormalDebug << {
75+
stripTestOnlyForBuild("normal", "debug")
76+
}
77+
78+
task stripTestOnlyNormalRelease << {
79+
stripTestOnlyForBuild("normal", "release")
80+
}
81+
82+
task stripTestOnlyReplicaDebug << {
83+
stripTestOnlyForBuild("replica", "debug")
84+
}
85+
86+
task stripTestOnlyReplicaRelease << {
87+
stripTestOnlyForBuild("replica", "release")
88+
}
89+
90+
tasks.whenTaskAdded { task ->
91+
if (task.name == "splitsDiscoveryTaskNormalDebug") {
92+
task.dependsOn stripTestOnlyNormalDebug
93+
} else if (task.name == "splitsDiscoveryTaskNormalRelease") {
94+
task.dependsOn stripTestOnlyNormalRelease
95+
} else if (task.name == "splitsDiscoveryTaskReplicaDebug") {
96+
task.dependsOn stripTestOnlyReplicaDebug
97+
} else if (task.name == "splitsDiscoveryTaskReplicaRelease") {
98+
task.dependsOn stripTestOnlyReplicaRelease
99+
}
100+
}
101+
}
102+
103+
// Modifies the specified build variant's intermediate AndroidManifest.xml file
104+
// to remove the testOnly attribute. Used for preventing Gradle from inserting the
105+
// attribute when TestDPC is compiled with a development Android branch.
106+
private void stripTestOnlyForBuild(flavor, buildType) {
107+
def manifestPath =
108+
"${buildDir}/intermediates/manifests/full/${flavor}/${buildType}/AndroidManifest.xml"
109+
if (file(manifestPath).exists()) {
110+
def xml = new XmlParser().parse(manifestPath)
111+
def attributeNames = xml.application[0].attributes().collect { it.key }
112+
attributeNames.each {
113+
if (it.getLocalPart() == 'testOnly') {
114+
xml.application[0].attributes().remove(it)
115+
println "Removed testOnly attribute successfully!"
116+
}
117+
}
118+
new XmlNodePrinter(new PrintWriter(new FileWriter(manifestPath))).print(xml)
119+
}
73120
}
74121

75122
dependencies {

app/src/main/AndroidManifest.xml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2828
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
2929
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
30+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
31+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
32+
<uses-permission android:name="android.permission.REQUEST_PASSWORD_COMPLEXITY"/>
3033

3134
<application
3235
android:allowBackup="true"
@@ -69,7 +72,13 @@
6972
<activity
7073
android:name=".FinalizeActivity"
7174
android:label="@string/app_name"
72-
android:theme="@style/SuwThemeGlifV3.Light"/>
75+
android:theme="@style/SuwThemeGlifV3.Light"
76+
android:permission="android.permission.BIND_DEVICE_ADMIN">
77+
<intent-filter>
78+
<action android:name="android.app.action.ADMIN_POLICY_COMPLIANCE" />
79+
<category android:name="android.intent.category.DEFAULT"/>
80+
</intent-filter>
81+
</activity>
7382

7483
<activity
7584
android:name=".cosu.EnableCosuActivity"
@@ -88,6 +97,15 @@
8897
</intent-filter>
8998
</activity>
9099

100+
<activity android:name=".provision.DpcLoginActivity"
101+
android:exported="true"
102+
android:permission="android.permission.BIND_DEVICE_ADMIN">
103+
<intent-filter>
104+
<action android:name="android.app.action.GET_PROVISIONING_MODE" />
105+
<category android:name="android.intent.category.DEFAULT"/>
106+
</intent-filter>
107+
</activity>
108+
91109
<activity
92110
android:name=".provision.ProvisioningSuccessActivity"
93111
android:theme="@android:style/Theme.NoDisplay">
@@ -114,6 +132,15 @@
114132
</intent-filter>
115133
</receiver>
116134

135+
<receiver
136+
android:name=".DelegatedAdminReceiver"
137+
android:permission="android.permission.BIND_DEVICE_ADMIN">
138+
<intent-filter>
139+
<action android:name="android.app.action.CHOOSE_PRIVATE_KEY_ALIAS"/>
140+
<action android:name="android.app.action.NETWORK_LOGS_AVAILABLE"/>
141+
</intent-filter>
142+
</receiver>
143+
117144
<receiver android:name=".BootReceiver"
118145
android:exported="false">
119146
<intent-filter>

app/src/main/java/com/afwsamples/testdpc/AddAccountActivity.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.afwsamples.testdpc;
1818

19+
import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;
20+
21+
import android.accounts.Account;
1922
import android.accounts.AccountManager;
2023
import android.accounts.AuthenticatorException;
2124
import android.accounts.OperationCanceledException;
@@ -34,9 +37,7 @@
3437
import android.widget.EditText;
3538
import android.widget.RadioGroup;
3639
import android.widget.Toast;
37-
3840
import com.android.setupwizardlib.GlifLayout;
39-
4041
import java.io.IOException;
4142

4243
/**
@@ -106,6 +107,10 @@ private void addAccount(String accountName) {
106107
if (mNextActivityIntent != null) {
107108
startActivity(mNextActivityIntent);
108109
}
110+
final Intent resultIntent = new Intent();
111+
resultIntent.putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE,
112+
new Account(accountNameAdded, GOOGLE_ACCOUNT_TYPE));
113+
setResult(RESULT_OK, resultIntent);
109114
finish();
110115
} catch (OperationCanceledException | AuthenticatorException
111116
| IOException e) {
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright (C) 2018 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.afwsamples.testdpc;
18+
19+
import android.annotation.TargetApi;
20+
import android.app.admin.DevicePolicyManager;
21+
import android.app.admin.NetworkEvent;
22+
import android.content.ComponentName;
23+
import android.content.Context;
24+
import android.os.AsyncTask;
25+
import android.os.Build;
26+
import android.os.Process;
27+
import android.preference.PreferenceManager;
28+
import android.support.v4.os.BuildCompat;
29+
import android.text.TextUtils;
30+
import android.util.Log;
31+
import android.widget.Toast;
32+
33+
import java.io.File;
34+
import java.io.FileOutputStream;
35+
import java.io.IOException;
36+
import java.io.OutputStream;
37+
import java.util.ArrayList;
38+
import java.util.Date;
39+
import java.util.List;
40+
41+
import static com.afwsamples.testdpc.policy.PolicyManagementFragment.OVERRIDE_KEY_SELECTION_KEY;
42+
43+
/**
44+
* A class that implements common logic to handle direct and delegated admin callbacks
45+
* from DeviceAdminReceiver and DelegatedAdminReceiver.
46+
*/
47+
public class CommonReceiverOperations {
48+
public static final String NETWORK_LOGS_FILE_PREFIX = "network_logs_";
49+
50+
private static final String TAG = "AdminReceiver";
51+
52+
public static String onChoosePrivateKeyAlias(Context context, int uid) {
53+
if (uid == Process.myUid()) {
54+
// Always show the chooser if we were the one requesting the cert.
55+
return null;
56+
}
57+
58+
String chosenAlias = PreferenceManager.getDefaultSharedPreferences(context)
59+
.getString(OVERRIDE_KEY_SELECTION_KEY, null);
60+
if (!TextUtils.isEmpty(chosenAlias)) {
61+
showToast(context, "Substituting private key alias: \"" + chosenAlias + "\"");
62+
return chosenAlias;
63+
} else {
64+
return null;
65+
}
66+
}
67+
68+
@TargetApi(Build.VERSION_CODES.O)
69+
public static void onNetworkLogsAvailable(Context context, ComponentName admin, long batchToken,
70+
int networkLogsCount) {
71+
Log.i(TAG, "onNetworkLogsAvailable(), batchToken: " + batchToken
72+
+ ", event count: " + networkLogsCount);
73+
74+
DevicePolicyManager dpm =
75+
(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
76+
List<NetworkEvent> events = null;
77+
try {
78+
events = dpm.retrieveNetworkLogs(admin, batchToken);
79+
} catch (SecurityException e) {
80+
Log.e(TAG,
81+
"Exception while retrieving network logs batch with batchToken: " + batchToken
82+
, e);
83+
}
84+
85+
if (events == null) {
86+
Log.e(TAG, "Failed to retrieve network logs batch with batchToken: " + batchToken);
87+
showToast(context, context.getString(
88+
R.string.on_network_logs_available_token_failure, batchToken));
89+
return;
90+
}
91+
92+
showToast(context,
93+
context.getString(R.string.on_network_logs_available_success, batchToken));
94+
95+
ArrayList<String> loggedEvents = new ArrayList<>();
96+
if (BuildCompat.isAtLeastP()) {
97+
for (NetworkEvent event : events) {
98+
loggedEvents.add(event.toString());
99+
}
100+
} else {
101+
events.forEach(event -> loggedEvents.add(event.toString()));
102+
}
103+
new EventSavingTask(context, batchToken, loggedEvents).execute();
104+
}
105+
106+
private static class EventSavingTask extends AsyncTask<Void, Void, Void> {
107+
108+
private Context mContext;
109+
private long mBatchToken;
110+
private List<String> mLoggedEvents;
111+
112+
public EventSavingTask(Context context, long batchToken, ArrayList<String> loggedEvents) {
113+
mContext = context;
114+
mBatchToken = batchToken;
115+
mLoggedEvents = loggedEvents;
116+
}
117+
118+
@Override
119+
protected Void doInBackground(Void... params) {
120+
Date timestamp = new Date();
121+
String filename = NETWORK_LOGS_FILE_PREFIX + mBatchToken + "_" + timestamp.getTime()
122+
+ ".txt";
123+
File file = new File(mContext.getExternalFilesDir(null), filename);
124+
try (OutputStream os = new FileOutputStream(file)) {
125+
for (String event : mLoggedEvents) {
126+
os.write((event + "\n").getBytes());
127+
}
128+
Log.d(TAG, "Saved network logs to file: " + filename);
129+
} catch (IOException e) {
130+
Log.e(TAG, "Failed saving network events to file" + filename, e);
131+
}
132+
return null;
133+
}
134+
}
135+
136+
private static void showToast(Context context, String message) {
137+
final String appName = context.getString(R.string.app_name);
138+
Toast.makeText(context, String.format("[%s] %s", appName, message),
139+
Toast.LENGTH_LONG)
140+
.show();
141+
}
142+
143+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2018 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.afwsamples.testdpc;
18+
19+
import android.annotation.TargetApi;
20+
import android.content.Context;
21+
import android.content.Intent;
22+
import android.net.Uri;
23+
import android.os.Build;
24+
25+
@TargetApi(Build.VERSION_CODES.Q)
26+
public class DelegatedAdminReceiver extends android.app.admin.DelegatedAdminReceiver {
27+
28+
@Override
29+
public String onChoosePrivateKeyAlias(Context context, Intent intent, int uid, Uri uri,
30+
String alias) {
31+
return CommonReceiverOperations.onChoosePrivateKeyAlias(context, uid);
32+
}
33+
34+
@Override
35+
public void onNetworkLogsAvailable(Context context, Intent intent, long batchToken,
36+
int networkLogsCount) {
37+
CommonReceiverOperations.onNetworkLogsAvailable(context, null, batchToken,
38+
networkLogsCount);
39+
}
40+
}

0 commit comments

Comments
 (0)