MountServiceIdler.java revision d417d625d244356bc770e2692fd59e754a72f59f
1d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate/*
2d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate * Copyright (C) 2014 The Android Open Source Project
3d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate *
4d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate * Licensed under the Apache License, Version 2.0 (the "License");
5d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate * you may not use this file except in compliance with the License.
6d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate * You may obtain a copy of the License at
7d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate *
8d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate *      http://www.apache.org/licenses/LICENSE-2.0
9d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate *
10d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate * Unless required by applicable law or agreed to in writing, software
11d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate * distributed under the License is distributed on an "AS IS" BASIS,
12d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate * See the License for the specific language governing permissions and
14d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate * limitations under the License.
15d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate */
16d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate
17d417d625d244356bc770e2692fd59e754a72f59fChristopher Tatepackage com.android.server;
18d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate
19d417d625d244356bc770e2692fd59e754a72f59fChristopher Tateimport android.app.maintenance.IdleService;
20d417d625d244356bc770e2692fd59e754a72f59fChristopher Tateimport android.util.Slog;
21d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate
22d417d625d244356bc770e2692fd59e754a72f59fChristopher Tatepublic class MountServiceIdler extends IdleService {
23d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate    private static final String TAG = "MountServiceIdler";
24d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate
25d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate    private Runnable mFinishCallback = new Runnable() {
26d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate        @Override
27d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate        public void run() {
28d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate            Slog.i(TAG, "Got mount service completion callback");
29d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate            finishIdle();
30d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate        }
31d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate    };
32d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate
33d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate    @Override
34d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate    public boolean onIdleStart() {
35d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate        // The mount service will run an fstrim operation asynchronously
36d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate        // on a designated separate thread, so we provide it with a callback
37d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate        // that lets us cleanly end our idle timeslice.  It's safe to call
38d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate        // finishIdle() from any thread.
39d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate        MountService ms = MountService.sSelf;
40d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate        if (ms != null) {
41d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate            ms.runIdleMaintenance(mFinishCallback);
42d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate        }
43d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate        return ms != null;
44d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate    }
45d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate
46d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate    @Override
47d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate    public void onIdleStop() {
48d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate    }
49d417d625d244356bc770e2692fd59e754a72f59fChristopher Tate}
50