1 /* 2 * Copyright 2019 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 androidx.enterprise.feedback; 18 19 import static androidx.enterprise.feedback.KeyedAppStatesReporter.APP_STATE_DATA; 20 import static androidx.enterprise.feedback.KeyedAppStatesReporter.APP_STATE_KEY; 21 import static androidx.enterprise.feedback.KeyedAppStatesReporter.APP_STATE_MESSAGE; 22 import static androidx.enterprise.feedback.KeyedAppStatesReporter.APP_STATE_SEVERITY; 23 24 import android.os.Bundle; 25 26 import androidx.enterprise.feedback.KeyedAppState.Severity; 27 28 import com.google.auto.value.AutoValue; 29 30 import org.jspecify.annotations.NonNull; 31 import org.jspecify.annotations.Nullable; 32 33 /** 34 * A keyed app state received from an app. This contains all of the information added by the app to 35 * the {@link KeyedAppState} as well as the packageName and timestamp added when the state 36 * was received. 37 */ 38 @AutoValue 39 public abstract class ReceivedKeyedAppState { 40 41 // Create a no-args constructor so it doesn't appear in current.txt ReceivedKeyedAppState()42 ReceivedKeyedAppState() {} 43 44 /** Create a {@link ReceivedKeyedAppStateBuilder}. */ builder()45 public static @NonNull ReceivedKeyedAppStateBuilder builder() { 46 return new AutoValue_ReceivedKeyedAppState.Builder(); 47 } 48 49 /** Assumes {@link KeyedAppState#isValid(Bundle)}. */ fromBundle(Bundle bundle, String packageName, long timestamp)50 static ReceivedKeyedAppState fromBundle(Bundle bundle, String packageName, long timestamp) { 51 if (!KeyedAppState.isValid(bundle)) { 52 throw new IllegalArgumentException("Bundle is not valid"); 53 } 54 55 return builder() 56 .setPackageName(packageName) 57 .setTimestamp(timestamp) 58 .setKey(bundle.getString(APP_STATE_KEY)) 59 .setSeverity(bundle.getInt(APP_STATE_SEVERITY)) 60 .setMessage(bundle.getString(APP_STATE_MESSAGE)) 61 .setData(bundle.getString(APP_STATE_DATA)) 62 .build(); 63 } 64 65 /** 66 * The name of the package which submitted the states. 67 * 68 * <p>This is automatically set to the correct value by the receiver; it is NOT self-reported by 69 * the app sending the feedback. 70 */ getPackageName()71 public abstract @NonNull String getPackageName(); 72 73 /** 74 * The unix timestamp, in milliseconds, when the states were received. 75 * 76 * <p>This is automatically set to the correct value by the receiver; it is NOT self-reported by 77 * the app sending the feedback. 78 */ getTimestamp()79 public abstract long getTimestamp(); 80 81 /** See {@link KeyedAppState#getKey()} */ getKey()82 public abstract @NonNull String getKey(); 83 84 /** See {@link KeyedAppState#getSeverity()} */ getSeverity()85 public abstract int getSeverity(); 86 87 /** See {@link KeyedAppState#getMessage()} */ getMessage()88 public abstract @Nullable String getMessage(); 89 90 /** See {@link KeyedAppState#getData()} */ getData()91 public abstract @Nullable String getData(); 92 93 /** The builder for {@link ReceivedKeyedAppState}. */ 94 @AutoValue.Builder 95 public abstract static class ReceivedKeyedAppStateBuilder { 96 97 // Create a no-args constructor so it doesn't appear in current.txt ReceivedKeyedAppStateBuilder()98 ReceivedKeyedAppStateBuilder() {} 99 100 /** Set {@link ReceivedKeyedAppState#getPackageName()}. */ setPackageName( @onNull String packageName)101 public abstract @NonNull ReceivedKeyedAppStateBuilder setPackageName( 102 @NonNull String packageName); 103 104 /** Set {@link ReceivedKeyedAppState#getTimestamp()}. */ setTimestamp(long timestamp)105 public abstract @NonNull ReceivedKeyedAppStateBuilder setTimestamp(long timestamp); 106 107 /** Set {@link ReceivedKeyedAppState#getKey()}. */ setKey(@onNull String key)108 public abstract @NonNull ReceivedKeyedAppStateBuilder setKey(@NonNull String key); 109 110 /** Set {@link ReceivedKeyedAppState#getSeverity()}. */ setSeverity(@everity int severity)111 public abstract @NonNull ReceivedKeyedAppStateBuilder setSeverity(@Severity int severity); 112 113 /** Set {@link ReceivedKeyedAppState#getMessage()}. */ setMessage(@ullable String message)114 public abstract @NonNull ReceivedKeyedAppStateBuilder setMessage(@Nullable String message); 115 116 /** Set {@link ReceivedKeyedAppState#getData()}. */ setData(@ullable String data)117 public abstract @NonNull ReceivedKeyedAppStateBuilder setData(@Nullable String data); 118 119 /** 120 * Instantiate the {@link ReceivedKeyedAppState}. 121 * 122 * <p>Assumes the key and severity are set. 123 */ build()124 public abstract @NonNull ReceivedKeyedAppState build(); 125 } 126 } 127