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