1 /*
2  * Copyright (C) 2022 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.carrierdefaultapp;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.telephony.TelephonyManager;
22 import android.webkit.JavascriptInterface;
23 
24 import com.android.phone.slice.SlicePurchaseController;
25 
26 /**
27  * Data boost web service flow interface allowing carrier websites to send responses back to the
28  * slice purchase application using JavaScript.
29  */
30 public class DataBoostWebServiceFlow {
31     @NonNull SlicePurchaseActivity mActivity;
32 
DataBoostWebServiceFlow(@onNull SlicePurchaseActivity activity)33     public DataBoostWebServiceFlow(@NonNull SlicePurchaseActivity activity) {
34         mActivity = activity;
35     }
36 
37     /**
38      * Interface method allowing the carrier website to get the premium capability
39      * that was requested to purchase.
40      *
41      * This can be called using the JavaScript below:
42      * <script type="text/javascript">
43      *     function getRequestedCapability() {
44      *         DataBoostWebServiceFlow.getRequestedCapability();
45      *     }
46      * </script>
47      */
48     @JavascriptInterface
getRequestedCapability()49     @TelephonyManager.PremiumCapability public int getRequestedCapability() {
50         return mActivity.mCapability;
51     }
52 
53     /**
54      * Interface method allowing the carrier website to notify the slice purchase application of
55      * a successful premium capability purchase and the duration for which the premium capability is
56      * purchased.
57      *
58      * This can be called using the JavaScript below:
59      * <script type="text/javascript">
60      *     function notifyPurchaseSuccessful(duration_ms_long = 0) {
61      *         DataBoostWebServiceFlow.notifyPurchaseSuccessful(duration_ms_long);
62      *     }
63      * </script>
64      *
65      * @param duration The duration for which the premium capability is purchased in milliseconds.
66      *                 NOTE: The duration parameter is not used.
67      */
68     @JavascriptInterface
notifyPurchaseSuccessful(long duration)69     public void notifyPurchaseSuccessful(long duration) {
70         mActivity.onPurchaseSuccessful();
71     }
72 
73     /**
74      * Interface method allowing the carrier website to notify the slice purchase application of
75      * a successful premium capability purchase.
76      *
77      * This can be called using the JavaScript below:
78      * <script type="text/javascript">
79      *     function notifyPurchaseSuccessful() {
80      *         DataBoostWebServiceFlow.notifyPurchaseSuccessful();
81      *     }
82      * </script>
83      */
84     @JavascriptInterface
notifyPurchaseSuccessful()85     public void notifyPurchaseSuccessful() {
86         mActivity.onPurchaseSuccessful();
87     }
88 
89     /**
90      * Interface method allowing the carrier website to notify the slice purchase application of
91      * a failed premium capability purchase.
92      *
93      * This can be called using the JavaScript below:
94      * <script type="text/javascript">
95      *     function notifyPurchaseFailed(failure_code = 0, failure_reason = "unknown") {
96      *         DataBoostWebServiceFlow.notifyPurchaseFailed();
97      *     }
98      * </script>
99      *
100      * @param failureCode The failure code.
101      * @param failureReason If the failure code is
102      *                      {@link SlicePurchaseController#FAILURE_CODE_UNKNOWN},
103      *                      the human-readable reason for failure.
104      */
105     @JavascriptInterface
notifyPurchaseFailed(@licePurchaseController.FailureCode int failureCode, @Nullable String failureReason)106     public void notifyPurchaseFailed(@SlicePurchaseController.FailureCode int failureCode,
107             @Nullable String failureReason) {
108         mActivity.onPurchaseFailed(failureCode, failureReason);
109     }
110 
111     /**
112      * Interface method allowing the carrier website to notify the slice purchase application that
113      * the service flow ended prematurely. This can be due to user action, an error in the
114      * web sheet logic, or an error on the network side.
115      *
116      * This can be called using the JavaScript below:
117      * <script type="text/javascript">
118      *     function dismissFlow() {
119      *         DataBoostWebServiceFlow.dismissFlow();
120      *     }
121      * </script>
122      */
123     @JavascriptInterface
dismissFlow()124     public void dismissFlow() {
125         mActivity.onDismissFlow();
126     }
127 }
128