Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2017 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.android.cts.util;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.net.Uri;
     22 import android.os.Parcel;
     23 import android.os.Parcelable;
     24 
     25 public class TestResult implements Parcelable {
     26     public static final String EXTRA_TEST_RESULT =
     27             "com.android.cts.ephemeraltest.EXTRA_TEST_RESULT";
     28     private static final String ACTION_START_ACTIVITY =
     29             "com.android.cts.ephemeraltest.START_ACTIVITY";
     30 
     31     private final String mPackageName;
     32     private final String mComponentName;
     33     private final String mMethodName;
     34     private final String mStatus;
     35     private final String mException;
     36     private final Intent mIntent;
     37     private final boolean mInstantAppPackageInfoExposed;
     38 
     39     public String getPackageName() {
     40         return mPackageName;
     41     }
     42 
     43     public String getComponentName() {
     44         return mComponentName;
     45     }
     46 
     47     public String getMethodName() {
     48         return mMethodName;
     49     }
     50 
     51     public String getStatus() {
     52         return mStatus;
     53     }
     54 
     55     public String getException() {
     56         return mException;
     57     }
     58 
     59     public boolean getEphemeralPackageInfoExposed() {
     60         return mInstantAppPackageInfoExposed;
     61     }
     62 
     63     public Intent getIntent() {
     64         return mIntent;
     65     }
     66 
     67     public static Builder getBuilder() {
     68         return new Builder();
     69     }
     70 
     71     public void broadcast(Context context) {
     72         final Intent broadcastIntent = new Intent(ACTION_START_ACTIVITY);
     73         broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
     74         broadcastIntent.addFlags(Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS);
     75         broadcastIntent.putExtra(EXTRA_TEST_RESULT, this);
     76         context.sendBroadcast(broadcastIntent);
     77     }
     78 
     79     public void startActivity(Context context, Uri uri) {
     80         final Intent broadcastIntent = new Intent(Intent.ACTION_VIEW);
     81         broadcastIntent.addCategory(Intent.CATEGORY_BROWSABLE);
     82         broadcastIntent.putExtra(EXTRA_TEST_RESULT, this);
     83         broadcastIntent.setData(uri);
     84         context.startActivity(broadcastIntent);
     85     }
     86 
     87     private TestResult(String packageName, String componentName, String methodName,
     88             String status, String exception, Intent intent,
     89             boolean ephemeralPackageInfoExposed) {
     90         mPackageName = packageName;
     91         mComponentName = componentName;
     92         mMethodName = methodName;
     93         mStatus = status;
     94         mException = exception;
     95         mIntent = intent;
     96         mInstantAppPackageInfoExposed = ephemeralPackageInfoExposed;
     97     }
     98 
     99     @Override
    100     public int describeContents() {
    101         return 0;
    102     }
    103 
    104     @Override
    105     public void writeToParcel(Parcel dest, int flags) {
    106         dest.writeString(mPackageName);
    107         dest.writeString(mComponentName);
    108         dest.writeString(mMethodName);
    109         dest.writeString(mStatus);
    110         dest.writeString(mException);
    111         dest.writeParcelable(mIntent, 0 /* flags */);
    112         dest.writeInt(mInstantAppPackageInfoExposed ? 1 : 0);
    113     }
    114 
    115     public static final Creator<TestResult> CREATOR = new Creator<TestResult>() {
    116         public TestResult createFromParcel(Parcel source) {
    117             return new TestResult(source);
    118         }
    119         public TestResult[] newArray(int size) {
    120             return new TestResult[size];
    121         }
    122     };
    123 
    124     private TestResult(Parcel source) {
    125         mPackageName = source.readString();
    126         mComponentName = source.readString();
    127         mMethodName = source.readString();
    128         mStatus = source.readString();
    129         mException = source.readString();
    130         mIntent = source.readParcelable(Object.class.getClassLoader());
    131         mInstantAppPackageInfoExposed = source.readInt() != 0;
    132     }
    133 
    134     public static class Builder {
    135         private String packageName;
    136         private String componentName;
    137         private String methodName;
    138         private String status;
    139         private String exception;
    140         private Intent intent;
    141         private boolean instantAppPackageInfoExposed;
    142 
    143         private Builder() {
    144         }
    145         public Builder setPackageName(String _packageName) {
    146             packageName = _packageName;
    147             return this;
    148         }
    149         public Builder setComponentName(String _componentName) {
    150             componentName = _componentName;
    151             return this;
    152         }
    153         public Builder setMethodName(String _methodName) {
    154             methodName = _methodName;
    155             return this;
    156         }
    157         public Builder setStatus(String _status) {
    158             status = _status;
    159             return this;
    160         }
    161         public Builder setException(String _exception) {
    162             exception = _exception;
    163             return this;
    164         }
    165         public Builder setEphemeralPackageInfoExposed(boolean _instantAppPackageInfoExposed) {
    166             instantAppPackageInfoExposed = _instantAppPackageInfoExposed;
    167             return this;
    168         }
    169         public Builder setIntent(Intent _intent) {
    170             intent = _intent;
    171             return this;
    172         }
    173         public TestResult build() {
    174             return new TestResult(packageName, componentName, methodName,
    175                     status, exception, intent, instantAppPackageInfoExposed);
    176         }
    177     }
    178 }
    179