• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016, 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 package com.android.managedprovisioning.model;
17 
18 import android.os.Parcel;
19 import android.os.Parcelable;
20 import android.os.PersistableBundle;
21 import com.android.internal.annotations.Immutable;
22 import com.android.managedprovisioning.common.PersistableBundlable;
23 import com.android.managedprovisioning.common.StoreUtils;
24 import java.io.File;
25 import java.io.IOException;
26 
27 /**
28  * Stores disclaimers information.
29  */
30 @Immutable
31 public class DisclaimersParam extends PersistableBundlable {
32     private static final String HEADER_KEY = "HEADER_KEY";
33     private static final String CONTENT_PATH_KEY = "CONTENT_PATH_KEY";
34 
35     public static final Parcelable.Creator<DisclaimersParam> CREATOR
36             = new Parcelable.Creator<DisclaimersParam>() {
37         @Override
38         public DisclaimersParam createFromParcel(Parcel in) {
39             return new DisclaimersParam(in);
40         }
41 
42         @Override
43         public DisclaimersParam[] newArray(int size) {
44             return new DisclaimersParam[size];
45         }
46     };
47 
48     public final Disclaimer[] mDisclaimers;
49 
DisclaimersParam(Builder builder)50     private DisclaimersParam(Builder builder) {
51         mDisclaimers = builder.mDisclaimers;
52     }
53 
DisclaimersParam(Parcel in)54     private DisclaimersParam(Parcel in) {
55         this(createBuilderFromPersistableBundle(
56                 PersistableBundlable.getPersistableBundleFromParcel(in)));
57     }
58 
fromPersistableBundle(PersistableBundle bundle)59     public static DisclaimersParam fromPersistableBundle(PersistableBundle bundle) {
60         return createBuilderFromPersistableBundle(bundle).build();
61     }
62 
createBuilderFromPersistableBundle(PersistableBundle bundle)63     private static Builder createBuilderFromPersistableBundle(PersistableBundle bundle) {
64         String[] headers = bundle.getStringArray(HEADER_KEY);
65         String[] contentPaths = bundle.getStringArray(CONTENT_PATH_KEY);
66         Builder builder = new Builder();
67         if (headers != null) {
68             // assume headers.length == contentPaths.length
69             Disclaimer[] disclaimers = new Disclaimer[headers.length];
70             for (int i = 0; i < headers.length; i++) {
71                 disclaimers[i] = new Disclaimer(headers[i], contentPaths[i]);
72             }
73             builder.setDisclaimers(disclaimers);
74         }
75         return builder;
76     }
77 
cleanUp()78     public void cleanUp() {
79         if (mDisclaimers != null) {
80             for(Disclaimer disclaimer : mDisclaimers) {
81                 new File(disclaimer.mContentFilePath).delete();
82             }
83         }
84     }
85 
86     @Override
toPersistableBundle()87     public PersistableBundle toPersistableBundle() {
88         final PersistableBundle bundle = new PersistableBundle();
89         if (mDisclaimers != null) {
90             String[] headers = new String[mDisclaimers.length];
91             String[] contentPaths = new String[mDisclaimers.length];
92             for (int i = 0; i < mDisclaimers.length; i++) {
93                 headers[i] = mDisclaimers[i].mHeader;
94                 contentPaths[i] = mDisclaimers[i].mContentFilePath;
95             }
96             bundle.putStringArray(HEADER_KEY, headers);
97             bundle.putStringArray(CONTENT_PATH_KEY, contentPaths);
98         }
99         return bundle;
100     }
101 
102     @Immutable
103     public static class Disclaimer {
104         public final String mHeader;
105         public final String mContentFilePath;
Disclaimer(String header, String contentFilePath)106         public Disclaimer(String header, String contentFilePath) {
107             mHeader = header;
108             mContentFilePath = contentFilePath;
109         }
110     }
111 
112     public final static class Builder {
113         private Disclaimer[] mDisclaimers;
114 
setDisclaimers(Disclaimer[] disclaimers)115         public Builder setDisclaimers(Disclaimer[] disclaimers) {
116             mDisclaimers = disclaimers;
117             return this;
118         }
119 
build()120         public DisclaimersParam build() {
121             return new DisclaimersParam(this);
122         }
123     }
124 }
125