Android Open Source - android-vf-balance Vf Balance Widget






From Project

Back to project page android-vf-balance.

License

The source code is released under:

Apache License

If you think the Android project android-vf-balance 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 2009 Alastair Porter// w  ww.j a va2s .  c o  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 com.github.alastair.vfbalance;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

public class VfBalanceWidget extends AppWidgetProvider {

  private static HttpClient httpClient = new DefaultHttpClient();
  
  private final static String TAG = "VfBalance";
  
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // For each loaded widget
    for (int i : appWidgetIds) {
      Log.d(TAG, "app id " + i);

          SharedPreferences prefs = context.getSharedPreferences(VfBalanceConfigure.PREFS_NAME, 0);
          String user = prefs.getString("user_" + i, "notset");
          String pass = prefs.getString("password_" + i, "notset");
          
          Log.d(TAG, "user " + user + ", pass " + pass);
      
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
          views.setTextViewText(R.id.textpos, "hi");
          views.setTextColor(R.id.textpos, R.color.black);
          
          Intent defineIntent = new Intent(context, ClickIntent.class);
          defineIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, i);
          PendingIntent pendingIntent = PendingIntent.getService(context,
                  0 /* no requestCode */, defineIntent, 0 /* no flags */);
          views.setOnClickPendingIntent(R.id.layout, pendingIntent);
          
          // Tell the widget manager
          appWidgetManager.updateAppWidget(i, views);
    }
  }
  
  /**
   * Delete the username/password when this widget is deleted.
   */
  @Override
  public void onDeleted(Context context, int[] appWidgetIds) {
    for (int i : appWidgetIds) {
      SharedPreferences.Editor prefs = context.getSharedPreferences(VfBalanceConfigure.PREFS_NAME, 0).edit();
      prefs.remove("user_" + i);
      prefs.remove("password_" + i);
      prefs.commit();
    }
  }
  
  /**
   * Workaround hook to run onDeleted when the widget is deleted.
   */
  @Override
  public void onReceive(Context context, Intent intent) {
      final String action = intent.getAction();
      if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
          final int appWidgetId = intent.getExtras().getInt
              (AppWidgetManager.EXTRA_APPWIDGET_ID,
                  AppWidgetManager.INVALID_APPWIDGET_ID);
          if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
              this.onDeleted(context, new int[] { appWidgetId });
          }
      } else {
          super.onReceive(context, intent);
      }

  } 
  
  public static class ClickIntent extends Service  {

    public void onStart(Intent intent, int startId) {
      try {
        Bundle extras = intent.getExtras();
        final int appWidgetId = extras.getInt
            (AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        
        Log.d(TAG, "click for Widget ID: " + appWidgetId);

        SharedPreferences prefs = getBaseContext().getSharedPreferences(
            VfBalanceConfigure.PREFS_NAME, 0);
        String user = prefs.getString("user_" + appWidgetId, "");
        String pass = prefs.getString("password_" + appWidgetId, "");
        Log.d(TAG, "IN CLICK: user " + user + ", pass " + pass);
        
        if ("".equals(user) || "".equals(pass)) {
          // Not set, skip
          return;
        }

        ArrayList<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("pcode", "myvf"));
        nvps.add(new BasicNameValuePair("txtPassword", ""));
        nvps.add(new BasicNameValuePair("txtUserID",
            ""));
        nvps.add(new BasicNameValuePair("url", "/myvodafone"));
        nvps.add(new BasicNameValuePair("pinfo", ""));
        nvps.add(new BasicNameValuePair("txtKeyCode", ""));
        nvps.add(new BasicNameValuePair("template", ""));

        HttpPost httppost = new HttpPost(
            "https://www.vodafone.co.nz/knox/login_handler.jsp");
        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        HttpResponse response = httpClient.execute(httppost);
        Log.d(TAG, "post response: " + response.getStatusLine());
        HttpEntity entity = response.getEntity();
        entity.consumeContent();

        HttpGet get = new HttpGet(
            "https://www.vodafone.co.nz/manage_account_prepay/entermya.do");
        response = httpClient.execute(get);
        Log.d(TAG, "get response: " + response.getStatusLine());
        entity = response.getEntity();

        Scanner s = new Scanner(entity.getContent());
        String m = s.findWithinHorizon("([0-9]+\\.[0-9][0-9])", 0);
        Log.d(TAG, "Balance: " + m);
        String tot = s.findWithinHorizon("([0-9]*)\\&nbsp\\;MB", 0);
        tot = tot.replace("&nbsp;MB", "");
        String left = s.findWithinHorizon("([0-9]*)\\&nbsp\\;MB", 0);
        left = left.replace("&nbsp;MB", "");
        Log.d(TAG, "Bandwidth: " + left + " / " + tot);
        s.close();
        entity.consumeContent();
      } catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    }
    
    @Override
    public IBinder onBind(Intent intent) {
      return null;
    }
  }
}




Java Source Code List

com.github.alastair.vfbalance.VfBalanceConfigure.java
com.github.alastair.vfbalance.VfBalanceWidget.java