15229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood/*
25229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood * Copyright (C) 2010 The Android Open Source Project
35229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood *
45229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood * Licensed under the Apache License, Version 2.0 (the "License");
55229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood * you may not use this file except in compliance with the License.
65229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood * You may obtain a copy of the License at
75229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood *
85229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood *      http://www.apache.org/licenses/LICENSE-2.0
95229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood *
105229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood * Unless required by applicable law or agreed to in writing, software
115229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood * distributed under the License is distributed on an "AS IS" BASIS,
125229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood * See the License for the specific language governing permissions and
145229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood * limitations under the License.
155229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood */
165229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwoodpackage com.android.quicksearchbox;
175229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood
185229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwoodimport org.json.JSONException;
195229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwoodimport org.json.JSONObject;
205229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood
215229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwoodimport android.util.Log;
225229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood
235229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwoodimport java.util.ArrayList;
245229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwoodimport java.util.Collection;
255229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwoodimport java.util.Iterator;
265229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood
275229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood/**
285229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood * SuggestionExtras taking values from a {@link JSONObject}.
295229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood */
305229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwoodpublic class JsonBackedSuggestionExtras implements SuggestionExtras {
315229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    private static final String TAG = "QSB.JsonBackedSuggestionExtras";
325229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood
335229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    private final JSONObject mExtras;
345229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    private final Collection<String> mColumns;
355229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood
365229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    public JsonBackedSuggestionExtras(String json) throws JSONException {
375229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        mExtras = new JSONObject(json);
385229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        mColumns = new ArrayList<String>(mExtras.length());
395229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        Iterator<String> it = mExtras.keys();
405229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        while (it.hasNext()) {
415229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood            mColumns.add(it.next());
425229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        }
435229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    }
445229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood
455229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    public JsonBackedSuggestionExtras(SuggestionExtras extras) throws JSONException {
465229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        mExtras = new JSONObject();
475229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        mColumns = extras.getExtraColumnNames();
485229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        for (String column : extras.getExtraColumnNames()) {
495229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood            String value = extras.getExtra(column);
505229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood            mExtras.put(column, value == null ? JSONObject.NULL : value);
515229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        }
525229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    }
535229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood
545229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    public String getExtra(String columnName) {
555229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        try {
565229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood            if (mExtras.isNull(columnName)) {
575229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood                return null;
585229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood            } else {
595229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood                return mExtras.getString(columnName);
605229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood            }
615229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        } catch (JSONException e) {
625229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood            Log.w(TAG, "Could not extract JSON extra", e);
635229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood            return null;
645229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        }
655229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    }
665229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood
675229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    public Collection<String> getExtraColumnNames() {
685229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        return mColumns;
695229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    }
705229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood
715229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    @Override
725229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    public String toString() {
735229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        return mExtras.toString();
745229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    }
755229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood
765229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    public String toJsonString() {
775229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood        return toString();
785229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood    }
795229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood
805229b06f00d20aac76cd8519b37f2a579d61c54fMathew Inwood}
81