Example usage for android.database Cursor getColumnIndex

List of usage examples for android.database Cursor getColumnIndex

Introduction

In this page you can find the example usage for android.database Cursor getColumnIndex.

Prototype

int getColumnIndex(String columnName);

Source Link

Document

Returns the zero-based index for the given column name, or -1 if the column doesn't exist.

Usage

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static boolean isCached(String resourcePath) {
    String[] projection = new String[] { Nodes.NODE_IS_CACHED };
    String selection = Nodes.NODE_RESOURCE_PATH + "=?";
    String[] selectionArgs = new String[] { resourcePath };
    Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    if (c != null) {
        try {//from w  w  w  .  j a  va2s  . c  o m
            if (c.moveToFirst()) {
                return c.getInt(c.getColumnIndex(Nodes.NODE_IS_CACHED)) != 0;
            }
        } finally {
            c.close();
        }
    }
    return false;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static long getVolumeGeneration(String resourcePath) {
    String[] projection = new String[] { Volumes.VOLUME_GENERATION };
    String selection = Volumes.VOLUME_RESOURCE_PATH + "=?";
    String[] selectionArgs = new String[] { resourcePath };
    Cursor c = sResolver.query(Volumes.CONTENT_URI, projection, selection, selectionArgs, null);
    if (c != null) {
        try {/*from  w w w.  j  a v  a  2  s. com*/
            if (c.moveToFirst()) {
                return c.getLong(c.getColumnIndex(Volumes.VOLUME_GENERATION));
            }
        } finally {
            c.close();
        }
    }
    return 0;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static Set<Integer> getRootNodeIds() {
    Set<Integer> ids = new TreeSet<Integer>();
    final String[] projection = new String[] { Nodes._ID };
    final String selection = Nodes.NODE_PARENT_PATH + " IS NULL";
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, null, null);
    try {//from   w  ww  .j av a2s .c o  m
        if (c.moveToFirst()) {
            int id;
            do {
                id = c.getInt(c.getColumnIndex(Nodes._ID));
                ids.add(id);
            } while (c.moveToNext());
        }
    } finally {
        c.close();
    }
    return ids;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

/**
 * For a given file {@link Uri} string, we check if its hash has a
 * corresponding entry in the {@link MetaProvider}, telling thus whether the
 * file from under given {@link Uri} string has been already uploaded.
 * /*from w ww .  j  a  v  a 2s  . co m*/
 * @param uriString
 *            the uri string which content we are checking
 * @return resourcePath if content under uri has been already uploaded,
 *         null otherwise
 */
public static String isUploaded(String uriString) {
    File file = null;
    String fileHash = null;

    if (uriString.startsWith(ContentResolver.SCHEME_CONTENT)) {
        final String[] projection = new String[] { MediaColumns.DATA };
        final Cursor c = sResolver.query(Uri.parse(uriString), projection, null, null, null);
        try {
            if (c.moveToFirst()) {
                String data = c.getString(c.getColumnIndex(MediaColumns.DATA));
                file = new File(data);
            } else {
                return null;
            }
        } finally {
            c.close();
        }
    } else if (uriString.startsWith(ContentResolver.SCHEME_FILE)) {
        final URI fileURI = URI.create(Uri.encode(uriString, ":/"));
        file = new File(fileURI);
    } else {
        Log.e(TAG, "Tried to check malformed uri string: " + uriString);
        return null;
    }

    try {
        if (file != null && file.exists()) {
            fileHash = HashUtils.getSha1(file);
            Log.d(TAG, String.format("Computed hash: '%s'", fileHash));
        } else {
            throw new FileNotFoundException("isUploaded()");
        }
    } catch (Exception e) {
        Log.e(TAG, "Can't compute file hash!", e);
        return null;
    }

    final String[] projection = new String[] { Nodes.NODE_RESOURCE_PATH };
    final String selection = Nodes.NODE_HASH + "=?";
    final String[] selectionArgs = new String[] { fileHash };
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    String resourcePath = null;
    try {
        if (c.moveToFirst()) {
            resourcePath = c.getString(c.getColumnIndex(Nodes.NODE_RESOURCE_PATH));
            Log.d(TAG, "Corresponding file hash found: " + resourcePath);
        } else {
            Log.d(TAG, "Corresponding file hash not found.");
        }
    } finally {
        c.close();
    }
    return resourcePath;
}

From source file:com.dongfang.dicos.sina.UtilSina.java

/**
 * Get a HttpClient object which is setting correctly .
 * //ww  w . j  a v  a2 s . com
 * @param context
 *            : context of activity
 * @return HttpClient: HttpClient object
 */
public static HttpClient getHttpClient(Context context) {
    BasicHttpParams httpParameters = new BasicHttpParams();
    // Set the default socket timeout (SO_TIMEOUT) // in
    // milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setConnectionTimeout(httpParameters, UtilSina.SET_CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParameters, UtilSina.SET_SOCKET_TIMEOUT);
    HttpClient client = new DefaultHttpClient(httpParameters);
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {
        // ??APN
        Uri uri = Uri.parse("content://telephony/carriers/preferapn");
        Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
        if (mCursor != null && mCursor.moveToFirst()) {
            // ???
            String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
            if (proxyStr != null && proxyStr.trim().length() > 0) {
                HttpHost proxy = new HttpHost(proxyStr, 80);
                client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
            }
            mCursor.close();
        }
    }
    return client;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static Set<Integer> getChildrenIds(String resourcePath) {
    Set<Integer> ids = new TreeSet<Integer>();
    final String[] projection = new String[] { Nodes._ID, Nodes.NODE_RESOURCE_STATE };
    final String selection = Nodes.NODE_PARENT_PATH + "=?";
    //+ " AND " + Nodes.NODE_RESOURCE_STATE + " IS NULL"; // FIXME
    final String[] selectionArgs = new String[] { resourcePath };
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    try {/*from   w  w  w  . jav  a 2s  .co m*/
        if (c.moveToFirst()) {
            int id;
            do {
                id = c.getInt(c.getColumnIndex(Nodes._ID));
                // We check the state, above SQL is failing to filter out
                // nodes which are in non-null state. No idea why.
                String s = c.getString(c.getColumnIndex(Nodes.NODE_RESOURCE_STATE));
                if (s == null) {
                    ids.add(id);
                } else {
                    Log.d("MetaUtilities", "child state != null, ignoring");
                }
            } while (c.moveToNext());
        }
    } finally {
        c.close();
    }
    return ids;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static Set<String> getUserNodePaths() {
    Set<String> userNodePaths = new TreeSet<String>();
    final String[] projection = new String[] { Nodes._ID, Nodes.NODE_RESOURCE_PATH };
    final String selection = Nodes.NODE_PARENT_PATH + " IS NULL";
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, null, null);
    try {/* w ww.  j  a  v a2 s  .c  om*/
        if (c.moveToFirst()) {
            String resourcePath;
            do {
                resourcePath = c.getString(c.getColumnIndex(Nodes.NODE_RESOURCE_PATH));
                userNodePaths.add(resourcePath);
            } while (c.moveToNext());
        }
    } finally {
        c.close();
    }
    return userNodePaths;
}

From source file:com.zzl.zl_app.cache.Utility.java

/**
 * Get a HttpClient object which is setting correctly .
 * /*from  w ww .  j  a va 2  s .c  om*/
 * @param context
 *            : context of activity
 * @return HttpClient: HttpClient object
 */
public static HttpClient getHttpClient(Context context) {
    BasicHttpParams httpParameters = new BasicHttpParams();
    // Set the default socket timeout (SO_TIMEOUT) // in
    // milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setConnectionTimeout(httpParameters, Utility.SET_CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParameters, Utility.SET_SOCKET_TIMEOUT);
    HttpClient client = new DefaultHttpClient(httpParameters);
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifiManager.getConnectionInfo();
    if (!wifiManager.isWifiEnabled() || -1 == info.getNetworkId()) {
        // ??APN?
        Uri uri = Uri.parse("content://telephony/carriers/preferapn");
        Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
        if (mCursor != null && mCursor.moveToFirst()) {
            // ???
            String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
            if (proxyStr != null && proxyStr.trim().length() > 0) {
                HttpHost proxy = new HttpHost(proxyStr, 80);
                client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
            }
            mCursor.close();
        }
    }
    return client;
}

From source file:com.dongfang.dicos.sina.UtilSina.java

public static HttpClient getNewHttpClient(Context context) {
    try {//from  w w  w  .ja  va  2  s .co  m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(params, 10000);
        HttpConnectionParams.setSoTimeout(params, 10000);

        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        // Set the default socket timeout (SO_TIMEOUT) // in
        // milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setConnectionTimeout(params, UtilSina.SET_CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, UtilSina.SET_SOCKET_TIMEOUT);
        HttpClient client = new DefaultHttpClient(ccm, params);
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (!wifiManager.isWifiEnabled()) {
            // ??APN
            Uri uri = Uri.parse("content://telephony/carriers/preferapn");
            Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                // ???
                String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
                if (proxyStr != null && proxyStr.trim().length() > 0) {
                    HttpHost proxy = new HttpHost(proxyStr, 80);
                    client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
                }
                mCursor.close();
            }
        }
        return client;
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:com.zzl.zl_app.cache.Utility.java

public static HttpClient getNewHttpClient(Context context) {
    try {//from  ww w.ja v  a  2 s  .  c om
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();

        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        // Set the default socket timeout (SO_TIMEOUT) // in
        // milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setConnectionTimeout(params, Utility.SET_CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, Utility.SET_SOCKET_TIMEOUT);
        HttpClient client = new DefaultHttpClient(ccm, params);

        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifiManager.getConnectionInfo();
        if (!wifiManager.isWifiEnabled() || -1 == info.getNetworkId()) {
            // ??APN?
            Uri uri = Uri.parse("content://telephony/carriers/preferapn");
            Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                // ???
                String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
                if (proxyStr != null && proxyStr.trim().length() > 0) {
                    HttpHost proxy = new HttpHost(proxyStr, 80);
                    client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
                }
                mCursor.close();
            }
        }
        return client;
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}