Android Open Source - MWM-for-Android-Gen1 Utils






From Project

Back to project page MWM-for-Android-Gen1.

License

The source code is released under:

Apache License

If you think the Android project MWM-for-Android-Gen1 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 (c) 2011 Meta Watch Ltd.                                       *
  *  www.MetaWatch.org                                                        *
  *                                                                           *
  =============================================================================
  *                                                                           *
  *  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.                                           *
  *                                                                           *
  *****************************************************************************/
/*ww w  .j av a  2 s.  c o  m*/
 /*****************************************************************************
  * Utils.java                                                                *
  * Utils                                                                     *
  * Different utils                                                           *
  *                                                                           *
  *                                                                           *
  *****************************************************************************/

package org.metawatch.manager;

import java.io.IOException;
import java.io.InputStream;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;

public class Utils {

  public static String getContactNameFromNumber(Context context, String number) {
    
    if (number.equals(""))
      return "Private number";

    String[] projection = new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER };
    Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    Cursor c = context.getContentResolver().query(contactUri, projection, null, null, null);
    
    if (c.moveToFirst()) {
      String name = c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME));

      if (name.length() > 0)
        return name;
      else
        return number;
    }
    
    return number;     
  }
  
  public static int getUnreadSmsCount(Context context) {

    int count = 0;

    Cursor cursor = context.getContentResolver().query(
        Uri.withAppendedPath(Uri.parse("content://sms"), "inbox"), 
        new String[] { "_id" }, 
        "read=0", 
        null, 
        null
      );
    
    if (cursor != null) {
      try {
        count = cursor.getCount();
      } finally {
        cursor.close();
      }
    }
    return count;
  }
  
  public static int getMissedCallsCount(Context context) {
    int missed = 0;
    try {
      Cursor cursor = context.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, null, null, null, null);
      cursor.moveToFirst();

      while (true) {
        if (cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE)) == 3)
          missed += cursor.getInt(cursor.getColumnIndex(CallLog.Calls.NEW));

        if (cursor.isLast())
          break;

        cursor.moveToNext();
      }

    } catch (Exception x) {
    }
    return missed;
  }
  
  public static int getUnreadGmailCount(Context context, String account, String label) {
    try {
      int nameColumn = 0;

      Cursor c = context.getContentResolver().query(Uri.parse("content://gmail-ls/labels/" + account), null, null, null, null);
      c.moveToFirst();

      for (int i = 0; i < c.getColumnCount(); i++)
        if (c.getColumnName(i).equals("canonicalName")) {
          nameColumn = i;
          break;
        }

      while (true) {
        if (c.getString(nameColumn).equals(label))
          for (int i = 0; i < c.getColumnCount(); i++) {
            if (c.getColumnName(i).equals("numUnreadConversations")) {
              return Integer.parseInt(c.getString(i));
            }
          }

        c.moveToNext();

        if (c.isLast()) {
          break;
        }
      }
    } catch (Exception x) {
      Log.d(MetaWatch.TAG, x.toString());
    }

    return 0;
  }
  
  public static String getGoogleAccountName(Context context) {
    AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = accountManager.getAccounts();
    int count = accounts.length;
    Account account = null;

    for (int i = 0; i < count; i++) {
      account = accounts[i];
      if (account.type.equals("com.google")) {
        return account.name;
      }
    }
    return "";
  }
  
  public static Bitmap loadBitmapFromAssets(Context context, String path) {
    try {
      InputStream inputStream = context.getAssets().open(path);
          Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
          inputStream.close();
          //Log.d(MetaWatch.TAG, "ok");
          return bitmap;
    } catch (IOException e) {
      //Log.d(MetaWatch.TAG, e.toString());
      return null;
    }
  }
  /*
  public static Bitmap loadBitmapFromPath(Context context, String path) {
      return BitmapFactory.decodeFile(path);
  }
  */
  
  public static String getVersion(Context context) {    
    try {
      PackageManager packageManager = context.getPackageManager();
      PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
      return packageInfo.versionName;
    } catch (NameNotFoundException e) {
    }
    return "unknown";
  }
  
  public static boolean isGmailAccessSupported(Context context) {
    
    
    try {
      PackageManager packageManager = context.getPackageManager();
      PackageInfo packageInfo = packageManager.getPackageInfo("com.google.android.gm", 0);
      // check for Gmail version earlier than v2.3.5 (169)
      if (packageInfo.versionCode < 169)
          return true;      
      
    } catch (NameNotFoundException e) {
    }
    
    
    return false;
  }
  

}




Java Source Code List

org.anddev.android.weatherforecast.weather.GoogleWeatherHandler.java
org.anddev.android.weatherforecast.weather.WeatherCurrentCondition.java
org.anddev.android.weatherforecast.weather.WeatherForecastCondition.java
org.anddev.android.weatherforecast.weather.WeatherSet.java
org.anddev.android.weatherforecast.weather.WeatherUtils.java
org.metawatch.manager.AlarmReceiver.java
org.metawatch.manager.ApiIntentReceiver.java
org.metawatch.manager.Application.java
org.metawatch.manager.BootUpReceiver.java
org.metawatch.manager.CallStateListener.java
org.metawatch.manager.CallVibrate.java
org.metawatch.manager.Call.java
org.metawatch.manager.DeviceSelection.java
org.metawatch.manager.GmailMonitor.java
org.metawatch.manager.Idle.java
org.metawatch.manager.ImageViewer.java
org.metawatch.manager.IntentReceiver.java
org.metawatch.manager.MediaControl.java
org.metawatch.manager.Message.java
org.metawatch.manager.MetaWatchService.java
org.metawatch.manager.MetaWatch.java
org.metawatch.manager.Monitors.java
org.metawatch.manager.NotificationBuilder.java
org.metawatch.manager.Notification.java
org.metawatch.manager.Protocol.java
org.metawatch.manager.Settings.java
org.metawatch.manager.TestSmsLoop.java
org.metawatch.manager.Test.java
org.metawatch.manager.Utils.java