• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 com.android.launcher3.util;
17 
18 import android.os.Looper;
19 
20 import androidx.annotation.WorkerThread;
21 
22 /**
23  * Utility class to define an object which does most of it's processing on a
24  * dedicated background thread.
25  */
26 public abstract class BgObjectWithLooper {
27 
28     /**
29      * Start initialization of the object
30      */
initializeInBackground(String threadName)31     public final void initializeInBackground(String threadName) {
32         new Thread(this::runOnThread, threadName).start();
33     }
34 
runOnThread()35     private void runOnThread() {
36         Looper.prepare();
37         onInitialized(Looper.myLooper());
38         Looper.loop();
39     }
40 
41     /**
42      * Called on the background thread to handle initialization
43      */
44     @WorkerThread
onInitialized(Looper looper)45     protected abstract void onInitialized(Looper looper);
46 }
47