Android Open Source - K6nele Package Name Registry






From Project

Back to project page K6nele.

License

The source code is released under:

Apache License

If you think the Android project K6nele listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2011, Institute of Cybernetics at Tallinn University of Technology
 */*from w w  w  .j  a  va  2  s.co m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ee.ioc.phon.android.speak;

import ee.ioc.phon.android.speak.provider.App;
import ee.ioc.phon.android.speak.provider.Grammar;
import ee.ioc.phon.android.speak.provider.Server;
import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

/**
 * <p>Database front-end that looks up the grammar and the server URLs
 * that correspond to the given app package name.
 * If the package name is <code>null</code>, then just sets the grammar and server IDs to 0
 * and ignores the database.
 * If the package name is not <code>null</code> and not in the database, then adds it there.
 * Increases the package counter (i.e. a number that shows how many times this lookup has been called).
 * 
 * <p>If the grammar ID is 0 then getGrammarUrl() returns <code>null</code>, otherwise returns the grammar URL.
 * If the server ID is 0 then getServerUrl() returns the default server URL, otherwise the listed URL.</p>
 * 
 * @author Kaarel Kaljurand
 */
public class PackageNameRegistry {

  private final Context mContext;
  private final long mGrammarId;
  private final long mServerId;


  public PackageNameRegistry(Context context, String packageName) {
    mContext = context;

    if (packageName == null) {
      mGrammarId = 0;
      mServerId = 0;
    } else {
      // Notice: we use query instead of managedQuery
      Cursor cursor = mContext.getContentResolver().query(
          App.Columns.CONTENT_URI,
          new String[] { App.Columns._ID, App.Columns.GRAMMAR, App.Columns.SERVER, App.Columns.COUNT },
          App.Columns.FNAME + "= ?",
          new String[] { packageName },
          null);

      if (cursor.moveToFirst()) {
        mGrammarId = cursor.getLong(cursor.getColumnIndex(App.Columns.GRAMMAR));
        mServerId = cursor.getLong(cursor.getColumnIndex(App.Columns.SERVER));
        long id = cursor.getLong(cursor.getColumnIndex(App.Columns._ID));
        int count = cursor.getInt(cursor.getColumnIndex(App.Columns.COUNT));

        ContentValues values = new ContentValues();
        values.put(App.Columns.COUNT, ++count);
        Uri appUri = ContentUris.withAppendedId(App.Columns.CONTENT_URI, id);
        mContext.getContentResolver().update(appUri, values, null, null);      
      } else {
        mGrammarId = 0;
        mServerId = 0;
        ContentValues values = new ContentValues();
        values.put(App.Columns.FNAME, packageName);
        values.put(App.Columns.COUNT, 1);
        mContext.getContentResolver().insert(App.Columns.CONTENT_URI, values);
      }
      cursor.close();
    }
  }

/*
  public PackageNameRegistry(Activity activity, String packageName) {
    mContext = activity.getBaseContext();

    if (packageName == null) {
      mGrammarId = 0;
      mServerId = 0;
    } else {
      Cursor cursor = activity.managedQuery(
          App.Columns.CONTENT_URI,
          new String[] { App.Columns._ID, App.Columns.GRAMMAR, App.Columns.SERVER, App.Columns.COUNT },
          App.Columns.FNAME + "= ?",
          new String[] { packageName },
          null);

      if (cursor.moveToFirst()) {
        mGrammarId = cursor.getLong(cursor.getColumnIndex(App.Columns.GRAMMAR));
        mServerId = cursor.getLong(cursor.getColumnIndex(App.Columns.SERVER));
        long id = cursor.getLong(cursor.getColumnIndex(App.Columns._ID));
        int count = cursor.getInt(cursor.getColumnIndex(App.Columns.COUNT));

        ContentValues values = new ContentValues();
        values.put(App.Columns.COUNT, ++count);
        Uri appUri = ContentUris.withAppendedId(App.Columns.CONTENT_URI, id);
        activity.getContentResolver().update(appUri, values, null, null);      
      } else {
        mGrammarId = 0;
        mServerId = 0;
        ContentValues values = new ContentValues();
        values.put(App.Columns.FNAME, packageName);
        values.put(App.Columns.COUNT, 1);
        activity.getContentResolver().insert(App.Columns.CONTENT_URI, values);
      }
      cursor.close();
    }
  }
*/

  public String getGrammarUrl() {
    if (mGrammarId == 0) {
      return null;
    }
    return Utils.idToValue(
        mContext,
        Grammar.Columns.CONTENT_URI,
        Grammar.Columns._ID,
        Grammar.Columns.URL,
        mGrammarId);
  }


  public String getGrammarLang() {
    if (mGrammarId == 0) {
      return null;
    }
    return Utils.idToValue(
        mContext,
        Grammar.Columns.CONTENT_URI,
        Grammar.Columns._ID,
        Grammar.Columns.LANG,
        mGrammarId);
  }


  public String getServerUrl() {
    if (mServerId == 0) {
      return null;
    }
    return Utils.idToValue(
        mContext,
        Server.Columns.CONTENT_URI,
        Server.Columns._ID,
        Server.Columns.URL,
        mServerId);
  }
}




Java Source Code List

ee.ioc.phon.android.speak.AboutActivity.java
ee.ioc.phon.android.speak.AppListActivity.java
ee.ioc.phon.android.speak.AppListCursorAdapter.java
ee.ioc.phon.android.speak.AudioCue.java
ee.ioc.phon.android.speak.AudioPauser.java
ee.ioc.phon.android.speak.Caller.java
ee.ioc.phon.android.speak.ChunkedWebRecSessionBuilder.java
ee.ioc.phon.android.speak.Constants.java
ee.ioc.phon.android.speak.DetailsActivity.java
ee.ioc.phon.android.speak.ExecutableString.java
ee.ioc.phon.android.speak.Executable.java
ee.ioc.phon.android.speak.Extras.java
ee.ioc.phon.android.speak.GetLanguageDetailsReceiver.java
ee.ioc.phon.android.speak.GrammarListActivity.java
ee.ioc.phon.android.speak.Log.java
ee.ioc.phon.android.speak.MicButton.java
ee.ioc.phon.android.speak.OnSwipeTouchListener.java
ee.ioc.phon.android.speak.PackageNameRegistry.java
ee.ioc.phon.android.speak.PreferencesRecognitionServiceHttp.java
ee.ioc.phon.android.speak.PreferencesRecognitionServiceWs.java
ee.ioc.phon.android.speak.Preferences.java
ee.ioc.phon.android.speak.RawAudioRecorder.java
ee.ioc.phon.android.speak.RecognizerIntentActivity.java
ee.ioc.phon.android.speak.RecognizerIntentListActivity.java
ee.ioc.phon.android.speak.RecognizerIntentService.java
ee.ioc.phon.android.speak.RecognizerIntent.java
ee.ioc.phon.android.speak.ServerListActivity.java
ee.ioc.phon.android.speak.SpeechRecognitionService.java
ee.ioc.phon.android.speak.Utils.java
ee.ioc.phon.android.speak.VoiceImeService.java
ee.ioc.phon.android.speak.VoiceImeView.java
ee.ioc.phon.android.speak.WebSocketRecognizer.java
ee.ioc.phon.android.speak.WebSocketResponse.java
ee.ioc.phon.android.speak.demo.AbstractRecognizerDemoActivity.java
ee.ioc.phon.android.speak.demo.ExtrasDemo.java
ee.ioc.phon.android.speak.demo.RepeaterDemo.java
ee.ioc.phon.android.speak.demo.SimpleDemo.java
ee.ioc.phon.android.speak.demo.VoiceSearchDemo.java
ee.ioc.phon.android.speak.provider.App.java
ee.ioc.phon.android.speak.provider.AppsContentProvider.java
ee.ioc.phon.android.speak.provider.BaseColumnsImpl.java
ee.ioc.phon.android.speak.provider.FileContentProvider.java
ee.ioc.phon.android.speak.provider.Grammar.java
ee.ioc.phon.android.speak.provider.Server.java