Android Open Source - survey_sdk_android Rewards Activity






From Project

Back to project page survey_sdk_android.

License

The source code is released under:

Apache License

If you think the Android project survey_sdk_android 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

package com.survey.android.view.themed;
//from  w  ww.  j  ava2s . c  o  m
import java.io.IOException;
import java.text.DecimalFormat;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.survey.android.R;
import com.survey.android.services.GeoSurveyPollService;
import com.survey.android.session.Configuration;
import com.survey.android.util.FontUtil;
import com.survey.android.util.Log;
import com.survey.android.util.Toiler;
import com.survey.android.util.FontUtil.Roboto;
import com.survey.android.util.Utility;
import com.survey.android.view.LocalizedFragmentActivity;
import com.survey.android.webclient.RestClient;

public class RewardsActivity extends LocalizedFragmentActivity{
  private static final String TAG = "RewardsActivity";
  
  protected ImageView ivSettings;  
  protected TextView tvUserName;  
  protected TextView tvCurrentBalance;
  protected TextView tvRewardsEarned;
  protected TextView tvPendingRewards;  
  protected TextView tvSurveysCompleted;
  protected WebView wvSweepstakes;
  
  private ProgressDialog pd;
  private String currentBalanceStored;
  private String careerEarningsStored;
  private String pendingRewardsStored;
  private int surveysCompletedStored;
  private int surveysAvailableStored;
  private String sweepstakesStored;
  private LinearLayout llCashInstruction;
  private LinearLayout llCashOut;  

  private AlertDialog alert;
  private Runnable alertRunnable;
  private Handler handler;
  Context context = this;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rewards);
    if (Configuration.isGeoPushEnabled()) {
      if (!Toiler.isServiceRunning(this, GeoSurveyPollService.class.getName())) { 
        startService(new Intent(this, GeoSurveyPollService.class));      
      }      
    }

    handler = new Handler();
    alertRunnable = null;

    initUI();
  }
  
  @Override
  public void onResume() {
    super.onResume();
    try {
      (new BackgroundDataTask()).execute();
      (new SweepstakesTask()).execute();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  @Override
  protected void onPause() {
    super.onPause();
    Toiler.hideTitleProgressBar(RewardsActivity.this);
  }
  
  @Override
  protected void onDestroy() {
    if (alert != null && alert.isShowing()) {
      alert.dismiss();
    }
    try {
      if (alertRunnable != null)
        handler.removeCallbacks(alertRunnable);
    } catch (Exception e) {
      e.printStackTrace();
    }
    try {
      if (pd != null) {
        pd.dismiss();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    super.onDestroy();
  }
  
  @Override
  public void onStart() {
    super.onStart();
  }
    
  @Override
  public void onStop() {
    super.onStart();  
  }
  
  @Override
  protected void initUI() {
    //FontUtil fu = new FontUtil(context);
    //fu.setRobotoFont(getWindow().getDecorView(), Roboto.NORMAL);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    currentBalanceStored = prefs.getString(context.getString(R.string.current_balance_stored), "0");
    careerEarningsStored = prefs.getString(context.getString(R.string.career_earnings_stored), "0");
    pendingRewardsStored = prefs.getString(context.getString(R.string.pending_rewards_stored), "0");
    surveysCompletedStored = prefs.getInt(context.getString(R.string.surveys_completed_stored), 0);
    surveysAvailableStored = prefs.getInt(context.getString(R.string.surveys_available_stored), 0);
    sweepstakesStored = prefs.getString(context.getString(R.string.sweepstakes_stored), "");
      
    llCashInstruction = (LinearLayout) findViewById(R.id.llCashInstruction);
    llCashOut = (LinearLayout) findViewById(R.id.llCashOut);      
    tvUserName = (TextView) findViewById(R.id.tvUserName);    
    tvCurrentBalance = (TextView) findViewById(R.id.tvCurrentBalance);
    tvRewardsEarned = (TextView) findViewById(R.id.tvRewardsEarned);
    tvPendingRewards = (TextView) findViewById(R.id.tvPendingRewards);    
    tvSurveysCompleted = (TextView) findViewById(R.id.tvSurveysCompleted);
    wvSweepstakes = (WebView) findViewById(R.id.wvSweepstakes);
    wvSweepstakes.setBackgroundColor(0);
    WebSettings settings = wvSweepstakes.getSettings();
    settings.setDefaultTextEncodingName("UTF-8");
    settings.setDefaultFontSize(18);
    
    String userName = prefs.getString(getString(R.string.username),"");
    tvUserName.setText(userName);

    if (Configuration.isShowLogoutButtonActive()) {
      ivSettings = (ImageView) findViewById(R.id.ivSettings);  
      ivSettings.setVisibility(View.VISIBLE);
      ivSettings.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          showPopup(v);        
        }
      });        
    }
    
    llCashOut.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        //startActivity(new Intent(context, CashoutActivity.class));
      }
    });    
    
    updateUI();
  }
  
  private void updateUI() {
    
    double currentBalance;
    try {
      currentBalance = Double.parseDouble(currentBalanceStored);
    } catch (Exception e) {
      currentBalance = 0;
      e.printStackTrace();
    }
    
    double pendingRewards;
    try {
      pendingRewards = Double.parseDouble(pendingRewardsStored);
    } catch (Exception e) {
      pendingRewards = 0;
      e.printStackTrace();
    }
    
    double careerEarning;
    try {
      careerEarning = Double.parseDouble(careerEarningsStored);
    } catch (Exception e) {
      careerEarning = 0;
      e.printStackTrace();
    }
    
//    // if balance is cashout eligible, make cashout layout visible
//    // and make it clickable. otherwise show cashout eligibility
//    // message
//    if (currentBalance >= 7.50) {
//      llCashOut.setVisibility(View.VISIBLE);
//      llCashOut.setClickable(true);
//    } else {
//      llCashInstruction.setVisibility(View.VISIBLE);              
//    }
    
    tvCurrentBalance.setText(Utility.parseDouble(currentBalance));
    
    tvPendingRewards.setText(Utility.parseDouble(pendingRewards));    

    tvRewardsEarned.setText(Utility.parseDouble(careerEarning));
    
    tvSurveysCompleted.setText("" + surveysCompletedStored);
    
    String sweepstakes = FontUtil.getHtmlWithRobotoFont(sweepstakesStored, Roboto.NORMAL, Roboto.Color.BLACK);
    wvSweepstakes.loadDataWithBaseURL(null, sweepstakes, "text/html", "UTF-8", null);
  }
  
  /**
   * Retrieves summary data and stores in shared preferences which is then displayed
   */   
  private class BackgroundDataTask extends AsyncTask<Void, Void, JSONObject> {

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected JSONObject doInBackground(Void... urls) {

      JSONObject result = null;
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
      String userToken = prefs.getString(context.getString(R.string.token), null);
      String userId = prefs.getString(context.getString(R.string.user_id), null);

      try {
        if (userToken != null && userId != null) {
          result = RestClient.getEarnings(userToken, userId);
        }
      } catch (JSONException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }
      return result;
    }

    @Override
    protected void onPostExecute(JSONObject result) {
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
      
      if (result != null) {
        try {
//          DecimalFormat twoDForm = new DecimalFormat("####.##");
//          twoDForm.setMinimumFractionDigits(2);

          String tempCurrentBalance = "0";
          String tempCareerEarning = "0";
          String tempPendingRewards = "0";
          int tempSurveysCompleted = 0;
          int tempSurveysAvailable = 0;

          if (result.has("current_balance")) {
            tempCurrentBalance = result
                .getString("current_balance");
          }
          if (result.has("career_earnings")) {
            tempCareerEarning = result.getString("career_earnings");
          }
          if (result.has("pending_rewards")) {
            tempPendingRewards = result
                .getString("pending_rewards");
          }
          if (result.has("surveys_completed")) {
            tempSurveysCompleted = result
                .getInt("surveys_completed");
          }
          if (result.has("surveys_available")) {
            tempSurveysAvailable = result
                .getInt("surveys_available");
          }

          if (!(tempCurrentBalance.equals(currentBalanceStored)
              && tempCareerEarning.equals(careerEarningsStored)
              && tempPendingRewards.equals(pendingRewardsStored)
              && tempSurveysCompleted == surveysCompletedStored && tempSurveysAvailable == surveysAvailableStored)) {

            Editor edit = prefs.edit();
            if (!tempCurrentBalance.equals(currentBalanceStored)) {
              currentBalanceStored = tempCurrentBalance;
              edit.putString(context.getString(R.string.current_balance_stored),currentBalanceStored);
            }
            if (!tempCareerEarning.equals(careerEarningsStored)) {
              careerEarningsStored = tempCareerEarning;
              edit.putString(context.getString(R.string.career_earnings_stored),careerEarningsStored);
            }
            if (!tempPendingRewards.equals(pendingRewardsStored)) {
              pendingRewardsStored = tempPendingRewards;
              edit.putString(context.getString(R.string.pending_rewards_stored),pendingRewardsStored);
            }
            if (tempSurveysCompleted != surveysCompletedStored) {
              surveysCompletedStored = tempSurveysCompleted;
              edit.putInt(context.getString(R.string.surveys_completed_stored),surveysCompletedStored);
            }
            if (tempSurveysAvailable != surveysAvailableStored) {
              surveysAvailableStored = tempSurveysAvailable;
              edit.putInt(context.getString(R.string.surveys_available_stored),surveysAvailableStored);
            }
            edit.commit();
            updateUI();
          }
        } catch (Exception exc) {
          exc.printStackTrace();
        }
      } 
    }
  }

  /**
   * Retrieves sweepstakes data and stores in shared preferences which is then displayed
   */   
  private class SweepstakesTask extends AsyncTask<Void, Void, String> {

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected String doInBackground(Void... urls) {

      String result = "";
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
      String userToken = prefs.getString(context.getString(R.string.token), null);

      try {
        if (userToken != null) {  
          result = RestClient.getSweepstakes(userToken, "sweepstakes");
        }

      } catch (JSONException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }
      return result;
    }

    @Override
    protected void onPostExecute(String result) {      
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
      Editor edit = prefs.edit();
      if (result != null) {
        String temp = result.replace("\"", "").replace("\\r", "").replace("Your sweepstakes entries:", "Your entries:");
        if (temp.length() <= 1) {
          sweepstakesStored = context.getString(R.string.no_sweepstakes);
        } else {
          sweepstakesStored =  Html.fromHtml(temp.replace("<br />", "BREAK")).toString().replace("BREAK", "<br />");
        }
        edit.putString(context.getString(R.string.sweepstakes_stored), sweepstakesStored);
      }
      edit.commit();
      updateUI();
    }
  }  
  
}




Java Source Code List

android.UnusedStub.java
com.google.android.gms.BuildConfig.java
com.google.android.gms.BuildConfig.java
com.survey.android.UnusedStub.java
com.survey.android.c2dm.C2DMRegistrationReceiver.java
com.survey.android.c2dm.C2DMTokenRefresher.java
com.survey.android.c2dm.GcmBroadcastReceiver.java
com.survey.android.c2dm.GcmNotificationReceiver.java
com.survey.android.c2dm.GcmRegistrationService.java
com.survey.android.c2dm.RegService.java
com.survey.android.common.PlacesAutoCompleteAdapter.java
com.survey.android.common.Themes.java
com.survey.android.containers.AppContainer.java
com.survey.android.containers.PollContainer.java
com.survey.android.custom_widgets.FontTextView.java
com.survey.android.custom_widgets.PollRatingsScaleLabeled.java
com.survey.android.custom_widgets.PollRatings.java
com.survey.android.custom_widgets.PollSelectionTable.java
com.survey.android.db.SerializationHelper.java
com.survey.android.db.SerializationManager.java
com.survey.android.fragment.AudioRecorderFragment.java
com.survey.android.geofence.GeofenceRemover.java
com.survey.android.geofence.GeofenceRequester.java
com.survey.android.geofence.GeofenceUtils.java
com.survey.android.geofence.LocationServiceErrorMessages.java
com.survey.android.geofence.ReceiveTransitionsIntentService.java
com.survey.android.geofence.SimpleGeofenceStore.java
com.survey.android.geofence.SimpleGeofence.java
com.survey.android.model.AnswerModel.java
com.survey.android.model.CategoryModel.java
com.survey.android.model.CurrentSectionModel.java
com.survey.android.model.Prefs.java
com.survey.android.model.QuestionModel.java
com.survey.android.model.ResponseModel.java
com.survey.android.model.SurveyModel.java
com.survey.android.model.UserModel.java
com.survey.android.services.BackgroundUploader.java
com.survey.android.services.DataBroadcastReceiver.java
com.survey.android.services.DeviceStartUpReceiver.java
com.survey.android.services.GeoSurveyPollService.java
com.survey.android.services.LocationTesterService.java
com.survey.android.services.ReferrerCatcher.java
com.survey.android.session.Configuration.java
com.survey.android.session.Session.java
com.survey.android.util.Base64.java
com.survey.android.util.ConstantData.java
com.survey.android.util.FontUtil.java
com.survey.android.util.GeoPush.java
com.survey.android.util.GeoTriggerBroadcastReceiver.java
com.survey.android.util.GeoTrigger.java
com.survey.android.util.LocationLog.java
com.survey.android.util.Log.java
com.survey.android.util.ScalingUtilities.java
com.survey.android.util.StrToIntMap.java
com.survey.android.util.StrToStrMap.java
com.survey.android.util.Toiler.java
com.survey.android.util.Utility.java
com.survey.android.util.WhiteLabel.java
com.survey.android.view.Gallery.java
com.survey.android.view.LocalizedFragmentActivity.java
com.survey.android.view.Main.java
com.survey.android.view.ThemeCustomizer.java
com.survey.android.view.themed.MainThemed.java
com.survey.android.view.themed.MessagesActivity.java
com.survey.android.view.themed.NotificationActivity.java
com.survey.android.view.themed.QuestionsActivity.java
com.survey.android.view.themed.RewardsActivity.java
com.survey.android.view.themed.SurveysActivity.java
com.survey.android.webclient.HttpRequest.java
com.survey.android.webclient.HttpsClient.java
com.survey.android.webclient.RestClient.java
com.survey.android.webclient.SurveyHttpClient.java
com.survey.android.webclient.SurveyRequest.java
com.survey.android.webclient.SurveySSLSocketFactory.java
com.survey.android.webclient.SurveyX509TrustManager.java
com.survey.android.widget.Widget.java
com.survey.androiddemo.AppContainer.java
com.survey.androiddemo.LoginActivity.java
com.survey.androiddemo.MainActivity.java
com.survey.androiddemo.SDKConfigSettings.java