SuggestionUtils.java revision 81a0897ff9685f3313c58294bf7973700c468b2b
13e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert/*
2fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert * Copyright (C) 2010 The Android Open Source Project
33e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
43e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
53e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * you may not use this file except in compliance with the License.
63e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * You may obtain a copy of the License at
73e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
83e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *      http://www.apache.org/licenses/LICENSE-2.0
93e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
103e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
113e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
123e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * See the License for the specific language governing permissions and
143e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * limitations under the License.
153e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert */
163e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
173e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringertpackage com.android.quicksearchbox;
183e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
1981a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringertimport android.app.SearchManager;
2081a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringertimport android.content.Intent;
2181a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringertimport android.net.Uri;
2281a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringertimport android.os.Bundle;
2381a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert
243e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
253e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert/**
26fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert * Base class for suggestion cursors.
273e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert */
283e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringertpublic abstract class AbstractSuggestionCursor implements SuggestionCursor {
293e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
303e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    private final String mUserQuery;
313e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
323e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    public AbstractSuggestionCursor(String userQuery) {
333e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert        mUserQuery = userQuery;
343e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
353e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
363e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    public String getUserQuery() {
373e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert        return mUserQuery;
383e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
393e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
4081a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert    public Intent getSuggestionIntent(Bundle appSearchData) {
4181a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        Source source = getSuggestionSource();
4281a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        String action = getSuggestionIntentAction();
4381a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        // use specific action if supplied, or default action if supplied, or fixed default
4481a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        if (action == null) {
4581a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert            action = source.getDefaultIntentAction();
4681a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert            if (action == null) {
4781a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert                action = Intent.ACTION_SEARCH;
4881a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert            }
4981a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        }
5081a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert
5181a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        String data = getSuggestionIntentDataString();
5281a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        String query = getSuggestionQuery();
5381a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        String userQuery = getUserQuery();
5481a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        String extraData = getSuggestionIntentExtraData();
5581a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert
5681a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        // Now build the Intent
5781a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        Intent intent = new Intent(action);
5881a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
5981a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        // We need CLEAR_TOP to avoid reusing an old task that has other activities
6081a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        // on top of the one we want.
6181a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
6281a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        if (data != null) {
6381a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert            intent.setData(Uri.parse(data));
6481a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        }
6581a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        intent.putExtra(SearchManager.USER_QUERY, userQuery);
6681a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        if (query != null) {
6781a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert            intent.putExtra(SearchManager.QUERY, query);
6881a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        }
6981a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        if (extraData != null) {
7081a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert            intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
7181a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        }
7281a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        if (appSearchData != null) {
7381a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert            intent.putExtra(SearchManager.APP_DATA, appSearchData);
7481a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        }
7581a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert
7681a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        intent.setComponent(source.getIntentComponent());
7781a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert        return intent;
7881a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert    }
7981a0897ff9685f3313c58294bf7973700c468b2bBjorn Bringert
8004a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert    public String getSuggestionDisplayQuery() {
8104a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert        String query = getSuggestionQuery();
8204a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert        if (query != null) {
8304a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert            return query;
8404a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert        }
8504a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert        Source source = getSuggestionSource();
8604a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert        if (source.shouldRewriteQueryFromData()) {
8704a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert            String data = getSuggestionIntentDataString();
8804a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert            if (data != null) {
8904a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert                return data;
9004a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert            }
9104a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert        }
9204a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert        if (source.shouldRewriteQueryFromText()) {
9304a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert            String text1 = getSuggestionText1();
9404a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert            if (text1 != null) {
9504a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert                return text1;
9604a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert            }
9704a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert        }
9804a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert        return null;
9904a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert    }
10004a180b52fb4100a2f3747e795fb5d26e3207a4aBjorn Bringert
1013e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert}
102