144f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer/*
244f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer * Copyright (c) 2016, The Android Open Source Project
344f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer *
444f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer * Licensed under the Apache License, Version 2.0 (the "License");
544f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer * you may not use this file except in compliance with the License.
644f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer * You may obtain a copy of the License at
744f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer *
844f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer *     http://www.apache.org/licenses/LICENSE-2.0
944f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer *
1044f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer * Unless required by applicable law or agreed to in writing, software
1144f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer * distributed under the License is distributed on an "AS IS" BASIS,
1244f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1344f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer * See the License for the specific language governing permissions and
1444f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer * limitations under the License.
1544f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer */
1644f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyerpackage com.android.car.radio;
1744f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer
1844f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyerimport android.content.AsyncTaskLoader;
1944f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyerimport android.content.Context;
2044f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyerimport android.support.annotation.NonNull;
2144f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyerimport com.android.car.radio.service.RadioStation;
2244f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer
2344f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyerimport java.util.Collections;
2444f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyerimport java.util.List;
2544f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer
2644f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer/**
2744f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer * An {@link AsyncTaskLoader} that will load all the pre-scanned radio stations for a given band.
2844f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer */
2944f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyerpublic class PreScannedChannelLoader extends AsyncTaskLoader<List<RadioStation>> {
3044f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    private final static int INVALID_RADIO_BAND = -1;
3144f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer
3244f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    private final RadioStorage mRadioStorage;
3344f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    private int mCurrentRadioBand = INVALID_RADIO_BAND;
3444f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer
3544f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    public PreScannedChannelLoader(Context context) {
3644f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer        super(context);
3744f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer        mRadioStorage = RadioStorage.getInstance(context);
3844f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    }
3944f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer
4044f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    /**
4144f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer     * Sets the radio band that this loader should load the pre-scanned stations for.
4244f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer     *
4344f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer     * @param radioBand One of the band values in {@link android.hardware.radio.RadioManager}. For
4444f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer     *                  example, {@link android.hardware.radio.RadioManager#BAND_FM}.
4544f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer     */
4644f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    public void setCurrentRadioBand(int radioBand) {
4744f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer        mCurrentRadioBand = radioBand;
4844f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    }
4944f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer
5044f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    /**
5144f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer     * Returns a list of {@link RadioStation}s representing the pre-scanned channels for the band
5244f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer     * specified by {@link #INVALID_RADIO_BAND}. If no stations exist for the given band, then an
5344f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer     * empty list is returned.
5444f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer     */
5544f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    @NonNull
5644f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    @Override
5744f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    public List<RadioStation> loadInBackground() {
5844f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer        if (mCurrentRadioBand == INVALID_RADIO_BAND) {
5944f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer            return Collections.emptyList();
6044f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer        }
6144f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer
6244f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer        return mRadioStorage.getPreScannedStationsForBand(mCurrentRadioBand);
6344f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer    }
6444f17dab6698b8c5d87672f5df71c471bd4b91a3Rakesh Iyer}
65