Here I have to us compass inside in fragment . My code for Compass fragment is.
public class AndroidCompassActivity extends SherlockFragment implements OnClickListener{ private static final String TAG = "AndroidCompassActivity";
private static WakeLock wakeLock = null;
private ShowDialogAsyncTask asyncTask;
private PopupWindow popUp;
private String questions="";
private String previousq="";
private String yesno="";
private static View compassView = null;
TextView compass,center;
Button yes, no;
String name, email, id, emailpref, pid;
View v;
int i = 0, len = 1;
JSONArray ar;
JSONObject problem;
JSONObject probs;
JSONArray jsonar = new JSONArray();
String problems;
public String zone;
CompassView cv;
private String data="";
ProgressDialog progress;
ProgressDialog progDialog,progDialog1;
private String reply = "";
private int height,width;
private ProgressBar askedquestion;
private int updateProgress=0;
private Context context;
Bundle bd=new Bundle();
Bundle bd1=new Bundle();
Fragment solution=new Solution();
Fragment homecenter=new HomeCenter();
Fragment findzone=new FindZone();
//AlertDialogManager alert = new AlertDialogManager();
//@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
View view=inflater.inflate(R.layout.compass, container,false);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Log.i(TAG, "onCreate()");
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
//context=getApplicationContext();
popUp = new PopupWindow(getActivity());
progDialog = new ProgressDialog(getActivity(),
R.style.Theme_MyDialog);
/*SharedPreferences sp = this.getSharedPreferences("Shared_email",
MODE_WORLD_READABLE);
emailpref = sp.getString("email", "nothing");*/
PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
pid = bd.getString("pid").toString();
email = bd.getString("email").toString();
data= bd.getString("data").toString();
try {
//System.out.println("munaj"+data);
/*if (data.equalsIgnoreCase("USER_NOT_REGISTERED")) {
Toast.makeText(getApplicationContext(),
"email id passed is not registered in database", 3000)
.show();
Intent i = new Intent(this, RegistrationClass.class);
startActivity(i);
SharedPreferences sp1 = getApplication().getSharedPreferences(
"Shared_email", MODE_WORLD_READABLE);
SharedPreferences.Editor edit = sp1.edit();
edit.putString("email", "nothing");
edit.commit();
} else*/ if (data.equalsIgnoreCase("INVALID_REQUEST")) {
Toast.makeText(
getActivity(),
"Passed problem id ,to be analysis have invalid value.",
3000).show();
} else {
JSONObject ob = new JSONObject(data);
String problem1 = ob.getString("problems");
if (problem1 == null) {
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.add(R.id.content_frame, solution);
ft.commit();
/*Intent i = new Intent(this, Solution.class);
startActivity(i);*/
} else {
ar = new JSONArray(problem1);
len = ar.length();
probs = ar.getJSONObject(0);
zone = probs.getString("zone");
name = probs.getString("name");
id = probs.getString("id");
System.out.println(id);
problem = new JSONObject();
problem.put("id", id);
problem.put("name", name);
problem.put("zone", zone);
cv = new CompassView(getActivity(), zone);
//setContentView(R.layout.compass);
// cv= new CompassView(getApplicationContext(),zone);
yes = (Button)getActivity(). findViewById(R.id.yes);
no = (Button) getActivity().findViewById(R.id.no);
yes.setOnClickListener(listener);
no.setOnClickListener(listener);
compass = (TextView)getActivity(). findViewById(R.id.compasstext);
compassView = getActivity().findViewById(R.id.compass);
center=(TextView)getActivity().findViewById(R.id.center);
SpannableString ss = new SpannableString("Please Stand at center of the House.");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
startActivity(new Intent(getActivity(), HomeCenter.class));
}
};
ss.setSpan(clickableSpan, 16, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(Color.BLUE), 16, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
center.setText(ss);
center.setMovementMethod(LinkMovementMethod.getInstance());
//set onclick on zone text to find the zone
String analysis="Do you have "
+ name
+ " in "
+ zone
+ " zone "
+ " ( Highlighted Above ).";
SpannableString ss1 = new SpannableString(analysis);
//ss.setSpan(new StyleSpan(Typeface.ITALIC), 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
int indexofzone=analysis.indexOf("zone");
ClickableSpan clickableSpan1 = new ClickableSpan() {
@Override
public void onClick(View textView) {
startActivity(new Intent(getActivity(), FindZone.class));
}
};
ss1.setSpan(clickableSpan1, indexofzone, indexofzone+4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss1.setSpan(new ForegroundColorSpan(Color.BLUE), indexofzone, indexofzone+4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
compass.setText(ss1);
compass.setMovementMethod(LinkMovementMethod.getInstance());
// cv.getzone(zone);
// progress.dismiss();
i++;
}
}
}catch (Exception e) {
e.printStackTrace();
}
return view;
}
private Object getWindowManager() {
// TODO Auto-generated method stub
return null;
}
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
final View view=v;
try {
if (view.getId() == R.id.yes) {
yesno="Yes";
} else {
yesno="No";
}
if (i < len+1) {
System.out.println(i+" "+len);
// jsonar.put(problem);
int donequest=100/len;
updateProgress=(i)*donequest;
if((100-updateProgress)<donequest)
updateProgress=(100-updateProgress)+updateProgress;
//will write code for food
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,
(ViewGroup) getActivity(). findViewById(R.id.popup_element));
popUp = new PopupWindow(layout, width, height, true);
//progress update
askedquestion=(ProgressBar)layout.findViewById(R.id.progressbar_Horizontal);
askedquestion.setProgress(updateProgress);
TextView status=(TextView)layout.findViewById(R.id.status);
status.setText((i)+"/"+len+" completed");
previousq=questions;
questions=questions+"\n\n"+i+". "+"Do you have "+ name+ " in "+ zone+ " zone.\n Your Answer : "+yesno;
TextView text=(TextView)layout.findViewById(R.id.infotext);
text.setText(questions);
text.setMovementMethod(new ScrollingMovementMethod());
Button back=(Button)layout.findViewById(R.id.backk);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View vi) {
// TODO Auto-generated method stub
popUp.dismiss();
questions=previousq;
}
});
Button ok=(Button)layout.findViewById(R.id.okk);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View vi) {
try{
if (view.getId() == R.id.yes) {
problem.put("exist", "yes");
} else {
problem.put("exist", "no");
}
jsonar.put(problem);
if(i<len){
popUp.dismiss();
JSONObject ob = ar.getJSONObject(i);
int getID = ob.getInt("id");
System.out.println("munnu "+i);
zone = ob.getString("zone");
name = ob.getString("name");
// setContentView(R.layout.compass);
cv = new CompassView( getActivity(), zone);
yes = (Button) getActivity(). findViewById(R.id.yes);
no = (Button) getActivity(). findViewById(R.id.no);
yes.setOnClickListener(listener);
compass = (TextView) getActivity().findViewById(R.id.compasstext);
SpannableString ss = new SpannableString("Please Stand at center of the House.");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
startActivity(new Intent( getActivity(), HomeCenter.class));
}
};
ss.setSpan(clickableSpan, 16, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(Color.BLUE), 16, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
center.setText(ss);
center.setMovementMethod(LinkMovementMethod.getInstance());
//set onclick on zone text to find the zone.
String analysis="Do you have "
+ name
+ " in "
+ zone
+ " zone "
+ " ( Highlighted Above ).";
SpannableString ss1 = new SpannableString(analysis);
int indexofzone=analysis.indexOf("zone");
ClickableSpan clickableSpan1 = new ClickableSpan() {
@Override
public void onClick(View textView) {
startActivity(new Intent( getActivity(), FindZone.class));
}
};
ss1.setSpan(clickableSpan1, indexofzone, indexofzone+4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss1.setSpan(new ForegroundColorSpan(Color.BLUE), indexofzone, indexofzone+4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
compass.setText(ss1);
compass.setMovementMethod(LinkMovementMethod.getInstance());
no.setOnClickListener(listener);
problem = new JSONObject();
problem.put("id", getID);
problem.put("name", name);
problem.put("zone", zone);
}else{
JSONObject obj = new JSONObject();
obj.put("elements", jsonar);
obj.put("problemId", pid);
obj.put("email", email);
problems = obj.toString();
//thread.start();
asyncTask = new ShowDialogAsyncTask();
asyncTask.execute();
cv =null;
}
i++;
}catch(Exception e){
e.getMessage();
}
}
});
popUp.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
/*else {
JSONObject obj = new JSONObject();
obj.put("elements", jsonar);
obj.put("problemId", pid);
obj.put("email", email);
problems = obj.toString();
// UtilityPost up = new UtilityPost();
// String url =
// "http://182.71.156.235:8080/Vastu/services/ws/submitresponse";//
// ,
// problems;
// rply = up
// .getResponse(
// "http://182.71.156.235:8080/Vastu/services/ws/submitresponse",
// problems);
thread.start();
asyncTask = new ShowDialogAsyncTask();
asyncTask.execute();
cv =null;
// Toast.makeText(getApplicationContext(), problems,
// 3000).show();
/*
* if (rply.equalsIgnoreCase("yes")) { Intent i = new
* Intent(this, Solution.class); i.putExtra("parentid", "1");
* i.putExtra("problem", problems); startActivity(i);
*
* } else if (rply.equalsIgnoreCase("USER_NOT_REGISTERED")) {
* Intent i = new Intent(this, RegistrationClass.class);
* startActivity(i); SharedPreferences sp1 = getApplication()
* .getSharedPreferences("Shared_email", MODE_WORLD_READABLE);
* SharedPreferences.Editor edit = sp1.edit();
* edit.putString("email", "nothing"); edit.commit(); } else {
* Toast.makeText(getApplicationContext(), "server error",
* 3000).show(); }
}*/
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}};
@Override
public void onDestroy() {
super.onDestroy();
compassView=null;
cv=null;
Log.i(TAG, "onDestroy()");
}
@Override
public void onStart() {
super.onStart();
//cv = new CompassView(getApplicationContext(), zone);
Log.i(TAG, "onStart()");
}
@Override
public void onStop() {
super.onStop();
//compassView=null;
//cv=null;
Log.i(TAG, "onStop()");
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume()");
wakeLock.acquire();
}
@Override
public void onPause() {
super.onPause();
Log.i(TAG, "onPause()");
wakeLock.release();
}
/*@Override
public void onSensorChanged(SensorEvent evt) {
super.onSensorChanged(evt);
if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER
|| evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
if (compassView != null)
compassView.postInvalidate();
}
//updateText(GlobalData.getBearing());
}*/
protected SensorEventListener sensorListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent evt) {
// let's broadcast compass data to any activity waiting for updates
/* Intent intent = new Intent(ACTION_COMPASS_UPDATES);
// packing azimuth value into bundle
Bundle bundle = new Bundle();
bundle.putFloat("azimuth", event.values[0]);
bundle.putFloat("pitch", event.values[1]);
bundle.putFloat("roll", event.values[2]);
intent.putExtras(bundle);
*/if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER || evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
if (compassView != null)
compassView.postInvalidate();
} }
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
};
private static void updateText(float bearing) {
int range = (int) (bearing / (360f / 16f));
String dirTxt = "";
if (range == 15 || range == 0)
dirTxt = "N";
else if (range == 1 || range == 2)
dirTxt = "NE";
else if (range == 3 || range == 4)
dirTxt = "E";
else if (range == 5 || range == 6)
dirTxt = "SE";
else if (range == 7 || range == 8)
dirTxt = "S";
else if (range == 9 || range == 10)
dirTxt = "SW";
else if (range == 11 || range == 12)
dirTxt = "W";
else if (range == 13 || range == 14)
dirTxt = "NW";
}
public void onBackPressed() {
final AlertDialog alertDialog = new AlertDialog.Builder(
getActivity()).create();
alertDialog.setMessage("You will lose your all Questions if go back.");
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
getActivity().finish();
}
});
alertDialog.setButton2("CANCEL",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
alertDialog.cancel();
}
});
alertDialog.show();
}
// Async task for waiting to get response
private class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void> {
// DataClassLentABook mData;
public int progress_status = 0;
@Override
protected void onPreExecute() {
super.onPreExecute();
progDialog.setMessage("Please wait...");
progDialog.show();
progress_status = 0;
}
@Override
protected Void doInBackground(Void... params) {
// String reply = "";
while (reply.equals("")) {
progress_status += 1;
UtilityPost up = new UtilityPost();
try {
reply = up
.getResponse(MainActivity.ipadress+
"submitresponse",
problems);
System.out.println(problems);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
publishProgress(progress_status);
SystemClock.sleep(300);
if (isCancelled()) {
// activateButton.setEnabled(true);
break;
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (progress_status == 90) {
progDialog.dismiss();
asyncTask.cancel(true);
final AlertDialog alertDialog = new AlertDialog.Builder(
getActivity()).create();
alertDialog.setMessage("Time Out Please try again");
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
alertDialog.dismiss();
Intent i = new Intent( getActivity(), MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
//finish();
Solution.flag=true;
}
});
alertDialog.show();
}
}
@Override
protected void onPostExecute(Void result) {
progDialog.dismiss();
if (reply.equalsIgnoreCase("yes")) {
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
//Bundle bd=new Bundle();
/*Intent i = new Intent(AndroidCompassActivity.this,
Solution.class);
i.putExtra("parentid", "1");
i.putExtra("problem", problems);
startActivity(i);*/
bd.putString("parentid", "1");
bd.putString("problem", problems);
solution.setArguments(bd);
ft.replace(R.id.content_frame, solution);
ft.commit();
popUp.dismiss();
} else if (reply.equalsIgnoreCase("USER_NOT_REGISTERED")) {
Intent i = new Intent( getActivity(),
RegistrationClass.class);
startActivity(i);
/*SharedPreferences sp1 = getActivity().getSharedPreferences(
"Shared_email", MODE_WORLD_READABLE);
SharedPreferences.Editor edit = sp1.edit();
edit.putString("email", "nothing");
edit.commit();*/
} else if(reply.equalsIgnoreCase("INVALID_REQUEST")) {
final AlertDialog alertDialog1 = new AlertDialog.Builder(
getActivity()).create();
alertDialog1.setMessage("Time Out Please try again");
alertDialog1.setButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
alertDialog1.dismiss();
getActivity().finish();
}
});
alertDialog1.show();
}
getActivity().finish();
}
}
/*Thread thread = new Thread(new Runnable() {
@Override
public void run() {
UtilityPost up = new UtilityPost();
try {
try {
reply = up
.getResponse(Home.ipadress+
"submitresponse",
problems);
System.out.println(problems);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}.
0 comments:
Post a Comment