Skip to content

Commit

Permalink
Merge /ssd/testdpc-qt-release/vendor/unbundled_google/packages/TestDP…
Browse files Browse the repository at this point in the history
…C into qt-1
  • Loading branch information
jscott1989 committed Apr 2, 2019
2 parents 3f316bf + 04c052c commit 6540da2
Show file tree
Hide file tree
Showing 52 changed files with 1,989 additions and 265 deletions.
57 changes: 52 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ apply plugin: 'com.android.application'
ext {
/* Version code for *next* release, bump *after* a release is created. */
// 1 or more digits
versionMajor = 5
versionMajor = 6
// exactly 1 digit
versionMinor = 0
// exactly 2 digits
versionBuild = 04
versionBuild = 00
}

android {
compileSdkVersion 28
compileSdkVersion 'android-Q'
buildToolsVersion "28.0.0"

defaultConfig {
applicationId "com.afwsamples.testdpc"
minSdkVersion 21
targetSdkVersion 28
targetSdkVersion 'Q'
versionCode versionMajor * 1000 + versionMinor * 100 + versionBuild
versionName "${versionMajor}.${versionMinor}.${versionBuild}"
multiDexEnabled true
Expand All @@ -43,7 +43,7 @@ android {

lintOptions {
check 'NewApi'
abortOnError true
abortOnError false
xmlReport false
textReport true
textOutput "stdout"
Expand All @@ -70,6 +70,53 @@ android {
output.assemble.dependsOn lintTask
}
}

task stripTestOnlyNormalDebug << {
stripTestOnlyForBuild("normal", "debug")
}

task stripTestOnlyNormalRelease << {
stripTestOnlyForBuild("normal", "release")
}

task stripTestOnlyReplicaDebug << {
stripTestOnlyForBuild("replica", "debug")
}

task stripTestOnlyReplicaRelease << {
stripTestOnlyForBuild("replica", "release")
}

tasks.whenTaskAdded { task ->
if (task.name == "splitsDiscoveryTaskNormalDebug") {
task.dependsOn stripTestOnlyNormalDebug
} else if (task.name == "splitsDiscoveryTaskNormalRelease") {
task.dependsOn stripTestOnlyNormalRelease
} else if (task.name == "splitsDiscoveryTaskReplicaDebug") {
task.dependsOn stripTestOnlyReplicaDebug
} else if (task.name == "splitsDiscoveryTaskReplicaRelease") {
task.dependsOn stripTestOnlyReplicaRelease
}
}
}

// Modifies the specified build variant's intermediate AndroidManifest.xml file
// to remove the testOnly attribute. Used for preventing Gradle from inserting the
// attribute when TestDPC is compiled with a development Android branch.
private void stripTestOnlyForBuild(flavor, buildType) {
def manifestPath =
"${buildDir}/intermediates/manifests/full/${flavor}/${buildType}/AndroidManifest.xml"
if (file(manifestPath).exists()) {
def xml = new XmlParser().parse(manifestPath)
def attributeNames = xml.application[0].attributes().collect { it.key }
attributeNames.each {
if (it.getLocalPart() == 'testOnly') {
xml.application[0].attributes().remove(it)
println "Removed testOnly attribute successfully!"
}
}
new XmlNodePrinter(new PrintWriter(new FileWriter(manifestPath))).print(xml)
}
}

dependencies {
Expand Down
29 changes: 28 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.REQUEST_PASSWORD_COMPLEXITY"/>

<application
android:allowBackup="true"
Expand Down Expand Up @@ -69,7 +72,13 @@
<activity
android:name=".FinalizeActivity"
android:label="@string/app_name"
android:theme="@style/SuwThemeGlifV3.Light"/>
android:theme="@style/SuwThemeGlifV3.Light"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<intent-filter>
<action android:name="android.app.action.ADMIN_POLICY_COMPLIANCE" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

<activity
android:name=".cosu.EnableCosuActivity"
Expand All @@ -88,6 +97,15 @@
</intent-filter>
</activity>

<activity android:name=".provision.DpcLoginActivity"
android:exported="true"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<intent-filter>
<action android:name="android.app.action.GET_PROVISIONING_MODE" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

<activity
android:name=".provision.ProvisioningSuccessActivity"
android:theme="@android:style/Theme.NoDisplay">
Expand All @@ -114,6 +132,15 @@
</intent-filter>
</receiver>

<receiver
android:name=".DelegatedAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<intent-filter>
<action android:name="android.app.action.CHOOSE_PRIVATE_KEY_ALIAS"/>
<action android:name="android.app.action.NETWORK_LOGS_AVAILABLE"/>
</intent-filter>
</receiver>

<receiver android:name=".BootReceiver"
android:exported="false">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.afwsamples.testdpc;

import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
Expand All @@ -34,9 +37,7 @@
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.android.setupwizardlib.GlifLayout;

import java.io.IOException;

/**
Expand Down Expand Up @@ -106,6 +107,10 @@ private void addAccount(String accountName) {
if (mNextActivityIntent != null) {
startActivity(mNextActivityIntent);
}
final Intent resultIntent = new Intent();
resultIntent.putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE,
new Account(accountNameAdded, GOOGLE_ACCOUNT_TYPE));
setResult(RESULT_OK, resultIntent);
finish();
} catch (OperationCanceledException | AuthenticatorException
| IOException e) {
Expand Down
143 changes: 143 additions & 0 deletions app/src/main/java/com/afwsamples/testdpc/CommonReceiverOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* 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.afwsamples.testdpc;

import android.annotation.TargetApi;
import android.app.admin.DevicePolicyManager;
import android.app.admin.NetworkEvent;
import android.content.ComponentName;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Process;
import android.preference.PreferenceManager;
import android.support.v4.os.BuildCompat;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.afwsamples.testdpc.policy.PolicyManagementFragment.OVERRIDE_KEY_SELECTION_KEY;

/**
* A class that implements common logic to handle direct and delegated admin callbacks
* from DeviceAdminReceiver and DelegatedAdminReceiver.
*/
public class CommonReceiverOperations {
public static final String NETWORK_LOGS_FILE_PREFIX = "network_logs_";

private static final String TAG = "AdminReceiver";

public static String onChoosePrivateKeyAlias(Context context, int uid) {
if (uid == Process.myUid()) {
// Always show the chooser if we were the one requesting the cert.
return null;
}

String chosenAlias = PreferenceManager.getDefaultSharedPreferences(context)
.getString(OVERRIDE_KEY_SELECTION_KEY, null);
if (!TextUtils.isEmpty(chosenAlias)) {
showToast(context, "Substituting private key alias: \"" + chosenAlias + "\"");
return chosenAlias;
} else {
return null;
}
}

@TargetApi(Build.VERSION_CODES.O)
public static void onNetworkLogsAvailable(Context context, ComponentName admin, long batchToken,
int networkLogsCount) {
Log.i(TAG, "onNetworkLogsAvailable(), batchToken: " + batchToken
+ ", event count: " + networkLogsCount);

DevicePolicyManager dpm =
(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
List<NetworkEvent> events = null;
try {
events = dpm.retrieveNetworkLogs(admin, batchToken);
} catch (SecurityException e) {
Log.e(TAG,
"Exception while retrieving network logs batch with batchToken: " + batchToken
, e);
}

if (events == null) {
Log.e(TAG, "Failed to retrieve network logs batch with batchToken: " + batchToken);
showToast(context, context.getString(
R.string.on_network_logs_available_token_failure, batchToken));
return;
}

showToast(context,
context.getString(R.string.on_network_logs_available_success, batchToken));

ArrayList<String> loggedEvents = new ArrayList<>();
if (BuildCompat.isAtLeastP()) {
for (NetworkEvent event : events) {
loggedEvents.add(event.toString());
}
} else {
events.forEach(event -> loggedEvents.add(event.toString()));
}
new EventSavingTask(context, batchToken, loggedEvents).execute();
}

private static class EventSavingTask extends AsyncTask<Void, Void, Void> {

private Context mContext;
private long mBatchToken;
private List<String> mLoggedEvents;

public EventSavingTask(Context context, long batchToken, ArrayList<String> loggedEvents) {
mContext = context;
mBatchToken = batchToken;
mLoggedEvents = loggedEvents;
}

@Override
protected Void doInBackground(Void... params) {
Date timestamp = new Date();
String filename = NETWORK_LOGS_FILE_PREFIX + mBatchToken + "_" + timestamp.getTime()
+ ".txt";
File file = new File(mContext.getExternalFilesDir(null), filename);
try (OutputStream os = new FileOutputStream(file)) {
for (String event : mLoggedEvents) {
os.write((event + "\n").getBytes());
}
Log.d(TAG, "Saved network logs to file: " + filename);
} catch (IOException e) {
Log.e(TAG, "Failed saving network events to file" + filename, e);
}
return null;
}
}

private static void showToast(Context context, String message) {
final String appName = context.getString(R.string.app_name);
Toast.makeText(context, String.format("[%s] %s", appName, message),
Toast.LENGTH_LONG)
.show();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.afwsamples.testdpc;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

@TargetApi(Build.VERSION_CODES.Q)
public class DelegatedAdminReceiver extends android.app.admin.DelegatedAdminReceiver {

@Override
public String onChoosePrivateKeyAlias(Context context, Intent intent, int uid, Uri uri,
String alias) {
return CommonReceiverOperations.onChoosePrivateKeyAlias(context, uid);
}

@Override
public void onNetworkLogsAvailable(Context context, Intent intent, long batchToken,
int networkLogsCount) {
CommonReceiverOperations.onNetworkLogsAvailable(context, null, batchToken,
networkLogsCount);
}
}
Loading

0 comments on commit 6540da2

Please sign in to comment.