diff options
3 files changed, 122 insertions, 0 deletions
diff --git a/packages/ExtServices/AndroidManifest.xml b/packages/ExtServices/AndroidManifest.xml index ff70e9712bcc..010a810cd791 100644 --- a/packages/ExtServices/AndroidManifest.xml +++ b/packages/ExtServices/AndroidManifest.xml @@ -63,6 +63,13 @@ android:resource="@array/autofill_field_classification_available_algorithms" /> </service> + <service android:name=".sms.FinancialSmsServiceImpl" + android:permission="android.permission.BIND_FINANCIAL_SMS_SERVICE"> + <intent-filter> + <action android:name="android.service.sms.action.FINANCIAL_SERVICE_INTENT" /> + </intent-filter> + </service> + <library android:name="android.ext.services"/> </application> diff --git a/packages/ExtServices/src/android/ext/services/sms/FinancialSmsServiceImpl.java b/packages/ExtServices/src/android/ext/services/sms/FinancialSmsServiceImpl.java new file mode 100644 index 000000000000..ab71802102ae --- /dev/null +++ b/packages/ExtServices/src/android/ext/services/sms/FinancialSmsServiceImpl.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.ext.services.sms; + +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.database.Cursor; +import android.database.CursorWindow; +import android.net.Uri; +import android.os.Bundle; +import android.service.sms.FinancialSmsService; +import android.util.Log; + +import java.util.ArrayList; +/** + * Service to provide financial apps access to sms messages. + */ +public class FinancialSmsServiceImpl extends FinancialSmsService { + + private static final String TAG = "FinancialSmsServiceImpl"; + private static final String KEY_COLUMN_NAMES = "column_names"; + + @Nullable + @Override + public CursorWindow onGetSmsMessages(@NonNull Bundle params) { + ArrayList<String> columnNames = params.getStringArrayList(KEY_COLUMN_NAMES); + if (columnNames == null || columnNames.size() <= 0) { + return null; + } + + Uri inbox = Uri.parse("content://sms/inbox"); + + try (Cursor cursor = getContentResolver().query(inbox, null, null, null, null); + CursorWindow window = new CursorWindow("FinancialSmsMessages")) { + int messageCount = cursor.getCount(); + if (messageCount > 0 && cursor.moveToFirst()) { + window.setNumColumns(columnNames.size()); + for (int row = 0; row < messageCount; row++) { + if (!window.allocRow()) { + Log.e(TAG, "CursorWindow ran out of memory."); + return null; + } + for (int col = 0; col < columnNames.size(); col++) { + String columnName = columnNames.get(col); + int inboxColumnIndex = cursor.getColumnIndexOrThrow(columnName); + String inboxColumnValue = cursor.getString(inboxColumnIndex); + boolean addedToCursorWindow = window.putString(inboxColumnValue, row, col); + if (!addedToCursorWindow) { + Log.e(TAG, "Failed to add:" + + inboxColumnValue + + ";column:" + + columnName); + return null; + } + } + cursor.moveToNext(); + } + } else { + Log.w(TAG, "No sms messages."); + } + return window; + } catch (Exception e) { + Log.e(TAG, "Failed to get sms messages."); + return null; + } + } +} diff --git a/packages/ExtServices/tests/src/android/ext/services/sms/FinancialSmsServiceImplTest.java b/packages/ExtServices/tests/src/android/ext/services/sms/FinancialSmsServiceImplTest.java new file mode 100644 index 000000000000..12575a63d0ad --- /dev/null +++ b/packages/ExtServices/tests/src/android/ext/services/sms/FinancialSmsServiceImplTest.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.ext.services.sms; + +import static com.google.common.truth.Truth.assertThat; + +import android.os.Bundle; + +import org.junit.Test; + +/** + * Contains the base tests for FinancialSmsServiceImpl. + */ +public class FinancialSmsServiceImplTest { + + private final FinancialSmsServiceImpl mService = new FinancialSmsServiceImpl(); + + @Test + public void testOnGetSmsMessages_nullWithNoParamData() { + assertThat(mService.onGetSmsMessages(new Bundle())).isNull(); + } +} |