1 /*
2  * Copyright (C) 2018 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 android.service.sms;
17 
18 import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
19 
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.app.Service;
24 import android.content.Intent;
25 import android.database.CursorWindow;
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.os.IBinder;
29 import android.os.Looper;
30 import android.os.RemoteCallback;
31 import android.os.RemoteException;
32 
33 /**
34  * A service to support sms messages read for financial apps.
35  *
36  * {@hide}
37  */
38 @SystemApi
39 public abstract class FinancialSmsService extends Service {
40 
41     private static final String TAG = "FinancialSmsService";
42 
43     /**
44      * The {@link Intent} action that must be declared as handled by a service
45      * in its manifest for the system to recognize it as a quota providing
46      * service.
47      */
48     public static final String ACTION_FINANCIAL_SERVICE_INTENT =
49             "android.service.sms.action.FINANCIAL_SERVICE_INTENT";
50 
51     /** {@hide} **/
52     public static final String EXTRA_SMS_MSGS = "sms_messages";
53 
54     private FinancialSmsServiceWrapper mWrapper;
55 
getSmsMessages(RemoteCallback callback, Bundle params)56     private void getSmsMessages(RemoteCallback callback, Bundle params) {
57         final Bundle data = new Bundle();
58         CursorWindow smsMessages = onGetSmsMessages(params);
59         if (smsMessages != null) {
60             data.putParcelable(EXTRA_SMS_MSGS, smsMessages);
61         }
62         callback.sendResult(data);
63     }
64 
65     private final Handler mHandler = new Handler(Looper.getMainLooper(), null, true);
66 
67     /** @hide */
FinancialSmsService()68     public FinancialSmsService() {
69     }
70 
71     @Override
onCreate()72     public void onCreate() {
73         super.onCreate();
74         mWrapper = new FinancialSmsServiceWrapper();
75     }
76 
77     @Override
onBind(Intent intent)78     public IBinder onBind(Intent intent) {
79         return mWrapper;
80     }
81 
82     /**
83      * Get sms messages for financial apps.
84      *
85      * @param params parameters passed in by the calling app.
86      * @return the {@code CursorWindow} with all sms messages for the app to read.
87      *
88      * {@hide}
89      */
90     @Nullable
91     @SystemApi
onGetSmsMessages(@onNull Bundle params)92     public abstract CursorWindow onGetSmsMessages(@NonNull Bundle params);
93 
94     private final class FinancialSmsServiceWrapper extends IFinancialSmsService.Stub {
95         @Override
getSmsMessages(RemoteCallback callback, Bundle params)96         public void getSmsMessages(RemoteCallback callback, Bundle params) throws RemoteException {
97             mHandler.sendMessage(obtainMessage(
98                     FinancialSmsService::getSmsMessages,
99                     FinancialSmsService.this,
100                     callback, params));
101         }
102     }
103 
104 }
105