• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.launcher3.util.rule;
18 
19 import static com.android.launcher3.util.TestUtil.grantWriteSecurePermission;
20 
21 import android.app.Instrumentation;
22 import android.content.ContentResolver;
23 import android.provider.Settings;
24 import android.util.Log;
25 import android.view.ViewConfiguration;
26 
27 import androidx.test.InstrumentationRegistry;
28 
29 import org.junit.rules.TestRule;
30 import org.junit.runner.Description;
31 import org.junit.runners.model.Statement;
32 
33 public class ExtendedLongPressTimeoutRule implements TestRule {
34 
35     private static final String TAG = "ExtendedLongPressTimeoutRule";
36 
37     private static final float LONG_PRESS_TIMEOUT_MULTIPLIER = 10f;
38 
39     @Override
apply(Statement base, Description description)40     public Statement apply(Statement base, Description description) {
41         return new Statement() {
42             @Override
43             public void evaluate() throws Throwable {
44                 ContentResolver contentResolver = InstrumentationRegistry.getInstrumentation()
45                         .getContext()
46                         .getContentResolver();
47                 int prevLongPressTimeout = Settings.Secure.getInt(
48                         contentResolver,
49                         Settings.Secure.LONG_PRESS_TIMEOUT,
50                         ViewConfiguration.getLongPressTimeout());
51                 int newLongPressTimeout =
52                         (int) (prevLongPressTimeout * LONG_PRESS_TIMEOUT_MULTIPLIER);
53 
54                 try {
55                     Log.d(TAG, "In try-block: Setting long press timeout from "
56                             + prevLongPressTimeout + "ms to " + newLongPressTimeout + "ms");
57                     grantWriteSecurePermission();
58                     Settings.Secure.putInt(
59                             contentResolver,
60                             Settings.Secure.LONG_PRESS_TIMEOUT,
61                             (int) (prevLongPressTimeout * LONG_PRESS_TIMEOUT_MULTIPLIER));
62 
63                     base.evaluate();
64                 } catch (Exception e) {
65                     Log.e(TAG, "Error", e);
66                     throw e;
67                 } finally {
68                     Log.d(TAG, "In finally-block: resetting long press timeout to "
69                             + prevLongPressTimeout + "ms");
70                     grantWriteSecurePermission();
71                     Settings.Secure.putInt(
72                             contentResolver,
73                             Settings.Secure.LONG_PRESS_TIMEOUT,
74                             prevLongPressTimeout);
75                 }
76             }
77         };
78     }
79 }
80