Back to project page survey_sdk_android.
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.
package com.survey.android.view.themed; /*from ww w . ja v a 2 s. co m*/ import java.io.IOException; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.app.Dialog; import android.app.NotificationManager; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.survey.android.R; import com.survey.android.containers.AppContainer; import com.survey.android.model.ResponseModel; import com.survey.android.model.SurveyModel; import com.survey.android.session.Configuration; import com.survey.android.util.ConstantData; import com.survey.android.util.FontUtil; import com.survey.android.util.FontUtil.Roboto; import com.survey.android.util.Log; import com.survey.android.util.Toiler; import com.survey.android.util.Utility; import com.survey.android.view.LocalizedFragmentActivity; import com.survey.android.webclient.RestClient; import com.survey.android.widget.Widget; //************************************************************************************************* public class NotificationActivity extends LocalizedFragmentActivity { public static final String TAG = "Notification"; private static int NOT_VIA_C2DM = -1; // indicate that user got here from // survey list, not by notification protected ImageView ivSettings; protected LinearLayout llNotificationHello; protected LinearLayout llSurveyRewards; protected TextView tvUserName; protected TextView tvAvailableMessage; protected TextView tvDetails; protected Button btnStartSurvey; // click on button loads first section protected Button btnRemove; protected Button btnOk; protected TextView tvSurveyTitle; protected WebView wvSurveyContent; // shows description for survey protected ImageView ivMoney; protected TextView tvMoney; protected ImageView ivNonCash; protected TextView tvNonCash; protected TextView tvTime; private String surveyTitle; // holds value of survey title private String surveyId; // holds value of survey id private int question_count; // holds value of question count private String token; // holds value of user token private JSONObject surveyInfo; private SurveyModel survey; private int notification_id; private int messagesAvailableStored; private Context context = NotificationActivity.this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); notification_id = getIntent().getIntExtra("notification_id",NOT_VIA_C2DM); messagesAvailableStored = getIntent().getIntExtra("messages_available_stored", 0); if (notification_id != NOT_VIA_C2DM) { String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); mNotificationManager.cancel(notification_id); try { Intent i = new Intent(this, Widget.class); i.setAction("android.appwidget.action.APPWIDGET_UPDATE"); this.sendBroadcast(i); } catch (Exception e) { e.printStackTrace(); } } initUI(); } @Override public void onStart() { super.onStart(); } @Override public void onStop() { super.onStart(); } @Override protected void initUI() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String userName = prefs.getString(getString(R.string.username),"").trim(); //FontUtil fu = new FontUtil(context); //fu.setRobotoFont(getWindow().getDecorView(), Roboto.NORMAL); // if arrived at this screen via app action, then hide notification message // and survey rewards llNotificationHello = (LinearLayout) findViewById(R.id.llNotificationHello); llSurveyRewards = (LinearLayout) findViewById(R.id.llSurveyRewards); if (notification_id == NOT_VIA_C2DM) { llNotificationHello.setVisibility(View.GONE); llSurveyRewards.setVisibility(View.GONE); } tvUserName = (TextView) findViewById(R.id.tvUserName); tvUserName.setText(String.format(getResources().getString(R.string.hi_user), userName)); tvAvailableMessage = (TextView) findViewById(R.id.tvAvailableMessage); tvDetails = (TextView) findViewById(R.id.tvDetails); btnStartSurvey = (Button) findViewById(R.id.btnStartSurvey); wvSurveyContent = (WebView) findViewById(R.id.wvSurveyContent); wvSurveyContent.setBackgroundColor(0); WebSettings settings = wvSurveyContent.getSettings(); settings.setDefaultTextEncodingName("UTF-8"); tvSurveyTitle = (TextView) findViewById(R.id.tvSurveyTitle); ivMoney = (ImageView) findViewById(R.id.ivMoney); tvMoney = (TextView) findViewById(R.id.tvMoney); ivNonCash = (ImageView) findViewById(R.id.ivNonCash); tvNonCash = (TextView) findViewById(R.id.tvNonCash); tvTime = (TextView) findViewById(R.id.tvTime); 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); } }); } try { surveyId = getIntent().getStringExtra("survey_id"); token = prefs.getString("token", null); btnStartSurvey.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { // for c2dm if (token == null) { DialogFragment newFragment = NotificationDialogFragment.newInstance( getString(R.string.start_survey_log_in), "",NotificationDialogFragment.DIALOG_START_SURVEY_LOG_IN); newFragment.setCancelable(false); newFragment.show(getSupportFragmentManager(), "dialog"); } else if (!Toiler .isNetworkAvailable(NotificationActivity.this)) { DialogFragment newFragment = NotificationDialogFragment.newInstance( getString(R.string.no_internet_connection_detected), NotificationDialogFragment.DIALOG_NO_INTERNET_CONNECTION); newFragment.setCancelable(false); newFragment.show(getSupportFragmentManager(), "dialog"); } else { (new ResponseIdTask()).execute(surveyId); } } catch (Exception e) { e.printStackTrace(); } } }); btnRemove = (Button) findViewById(R.id.btnRemove); btnRemove.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { RemoveSurveyFromUserList(surveyId); } }); btnOk = (Button) findViewById(R.id.btnOk); btnOk.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { NotificationActivity.this.finish(); } }); retrieveSurveyInfoFromServer(); } catch (Exception e) { e.printStackTrace(); } } protected void retrieveSurveyInfoFromServer() { (new LoadingDataTask()).execute(); } @Override protected void onPause() { super.onPause(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog"); if (prev != null) { ft.remove(prev); } ft.addToBackStack(null); } @Override protected void onDestroy() { super.onDestroy(); } /** * Removes message item from list */ private void RemoveSurveyFromUserList(String survey_id) { Log.d(TAG, "Removing survey_id from user: " + survey_id); // call api to remove survey item from user list (new RemoveItemTask()).execute(survey_id); } /** * Retrieves survey details and populates widgets for display */ private class LoadingDataTask extends AsyncTask<Void, Void, JSONObject> { @Override protected void onPreExecute() { super.onPreExecute(); DialogFragment newFragment = NotificationDialogFragment.newInstance( getString(R.string.loading), getString(R.string.please_wait), NotificationDialogFragment.DIALOG_LOADING); newFragment.setCancelable(false); newFragment.show(getSupportFragmentManager(), "dialog"); } @Override protected JSONObject doInBackground(Void... params) { try { surveyInfo = RestClient.getSurveyById(token, surveyId); if (surveyInfo != null) { survey = new SurveyModel(surveyInfo); } } catch (Exception e) { surveyInfo = null; e.printStackTrace(); } return surveyInfo; } /* (non-Javadoc) * @see android.os.AsyncTask#onPostExecute(java.lang.Object) */ @Override protected void onPostExecute(JSONObject result) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog"); if (prev != null) { ft.remove(prev); } if (result != null && survey != null) { try { tvSurveyTitle.setText(survey.getTitle().toUpperCase()); if (!Utility.isStringNullOrEmpty(survey.getDescription())) { String content = FontUtil.getHtmlWithRobotoFont(survey.getDescription(),Roboto.NORMAL,Roboto.Color.BLACK); wvSurveyContent.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null); } if (!Utility.isStringNullOrEmpty(survey.getSurvey_time())) { tvTime.setText(String.format(getResources().getString(R.string.survey_time), survey.getSurvey_time())); } if (surveyInfo.has("reward_cents")) { double reward = ((double) survey.getReward_cents()) / 100; if (reward > 0) { ivMoney.setVisibility(View.VISIBLE); tvMoney.setVisibility(View.VISIBLE); tvMoney.setText(getString(R.string.currency) + String.format(getResources().getConfiguration().locale, "%.2f", reward)); } } if (!Utility.isStringNullOrEmpty(survey.getReward_noncash())) { ivNonCash.setVisibility(View.VISIBLE); tvNonCash.setVisibility(View.VISIBLE); tvNonCash.setText(survey.getReward_noncash()); } question_count = survey.getQuestionCount(); // check if this survey is "message" type if (survey.getIsMsg() == true) { // get appropriate hello message tvAvailableMessage.setText(getResources().getString(R.string.message_available_msg)); // get appropriate Details label tvDetails.setText(getResources().getString(R.string.message_details)); // hide Start button btnStartSurvey.setVisibility(View.GONE); // display Ok button btnOk.setVisibility(View.VISIBLE); // display Remove button btnRemove.setVisibility(View.VISIBLE); // create survey response to record survey as viewed (new RecordViewTask()).execute(surveyId); } else { // show Start button btnStartSurvey.setVisibility(View.VISIBLE); } } catch (Exception e) { Log.e(TAG, "Exception: " + e); } //remove dialog fragment ft.commit(); } else { DialogFragment newFragment; newFragment = NotificationDialogFragment.newInstance(getString(R.string.no_internet_connection_detected), NotificationDialogFragment.DIALOG_NO_INTERNET_CONNECTION); newFragment.setCancelable(false); newFragment.show(ft, "dialog"); } super.onPostExecute(result); } } private class ResponseIdTask extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { DialogFragment newFragment = NotificationDialogFragment.newInstance( getString(R.string.starting), getString(R.string.please_wait), NotificationDialogFragment.DIALOG_LOADING); newFragment.setCancelable(false); newFragment.show(getSupportFragmentManager(), "dialog"); } @Override protected String doInBackground(String... surveyId) { String responseId = null; try { responseId = ResponseModel.remote(NotificationActivity.this, surveyId[0]).getId(); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return responseId; } @Override protected void onPostExecute(String responseId) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog"); if (prev != null) { ft.remove(prev); } ft.commit(); if (responseId != null) { NotificationActivity.this.startActivity(new Intent(NotificationActivity.this, QuestionsActivity.class).putExtra("response_id", responseId) .putExtra("question_count", question_count) .putExtra("survey_title", surveyTitle) .putExtra("survey_id", surveyId)); NotificationActivity.this.finish(); } } } private class RecordViewTask extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { } @Override protected String doInBackground(String... surveyId) { String responseId = null; try { responseId = ResponseModel.remote(NotificationActivity.this, surveyId[0]).getId(); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return responseId; } @Override protected void onPostExecute(String responseId) { if (responseId != null) { } } } /** * Removes message item */ private class RemoveItemTask extends AsyncTask<String, Void, Boolean> { @Override protected void onPreExecute() { DialogFragment newFragment = NotificationDialogFragment.newInstance( getString(R.string.loading), getString(R.string.please_wait), NotificationDialogFragment.DIALOG_LOADING); newFragment.setCancelable(false); newFragment.show(getSupportFragmentManager(), "dialog"); } @Override protected Boolean doInBackground(String... surveyId) { boolean deleted = false; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); try { String userToken = prefs.getString(getString(R.string.token), ""); deleted = RestClient.RemoveSurveyById(userToken, surveyId[0]); } catch (Exception e) { e.printStackTrace(); } return deleted; } @Override protected void onPostExecute(Boolean result) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog"); if (prev != null) { ft.remove(prev); } ft.commit(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); // update messages available count in shared preferences and display if (result) { if (messagesAvailableStored > 0) { messagesAvailableStored -= 1; Editor edit = prefs.edit(); edit.putInt(context.getString(R.string.messages_available_stored), messagesAvailableStored); edit.commit(); } } NotificationActivity.this.finish(); } } @SuppressLint("ValidFragment") private static class NotificationDialogFragment extends DialogFragment{ public static final int DIALOG_NO_INTERNET_CONNECTION = 0; public static final int DIALOG_LOADING = 1; protected static final int DIALOG_START_SURVEY_LOG_IN = 2; public static NotificationDialogFragment newInstance(String message, int tag) { NotificationDialogFragment frag = new NotificationDialogFragment(); Bundle args = new Bundle(); args.putString("message", message); args.putInt("tag", tag); frag.setArguments(args); return frag; } public static NotificationDialogFragment newInstance(String title, String message, int tag) { NotificationDialogFragment frag = newInstance(message, tag); frag.getArguments().putString("title", title); return frag; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog dialog = null; Bundle args = getArguments(); String message = args.getString("message"); int tag = args.getInt("tag"); AlertDialog.Builder builder = new AlertDialog.Builder( getActivity()); switch (tag) { case DIALOG_NO_INTERNET_CONNECTION: builder .setMessage(message) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int id) { ((NotificationActivity)getActivity()).retrieveSurveyInfoFromServer(); return; } }) .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int id) { getActivity().finish(); return; } }); dialog = builder.create(); break; case DIALOG_START_SURVEY_LOG_IN: builder .setTitle(args.getString("title")) .setPositiveButton( R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int id) { return; } }); dialog = builder.create(); break; case DIALOG_LOADING: dialog = new ProgressDialog(getActivity()); ((ProgressDialog) dialog).setMessage(message); ((ProgressDialog) dialog).setTitle(args.getString("title")); break; default: dialog = null; } return dialog; } } }