+++ /dev/null
-package com.example.furt.myapplication;
-
-import android.app.FragmentManager;
-import android.content.Context;
-import android.content.Intent;
-import android.content.res.Configuration;
-import android.os.Bundle;
-import android.support.v4.app.FragmentActivity;
-import android.util.DisplayMetrics;
-import android.util.TypedValue;
-import android.view.Gravity;
-import android.view.Menu;
-import android.view.MenuItem;
-import android.view.MotionEvent;
-import android.view.ScaleGestureDetector;
-import android.view.ViewGroup;
-import android.view.ViewTreeObserver;
-import android.widget.RelativeLayout;
-import android.widget.ScrollView;
-import android.widget.Toast;
-
-import java.sql.Timestamp;
-
-import static java.lang.Thread.sleep;
-
-public class DrawActivity extends FragmentActivity {
-
- static RelativeLayout globalHP; //layout delle ipotesi cancellate
- static ScaleGestureDetector scaleGestureDetector; //zoom listener
- static float spaceSize; //dimensione della spaziatura tra i sottoalberi
- static float textSize=40; //dimensione del font iniziale
- static Node selectedNode; //Node su cui è attualmente il focus
- static Node copiedNode;//nodo copiato
- static boolean addFakes;//booleano che segnala se bisogna aggiungere delle regole fake
- static Border[] b; //bordi per le borderedTextView
- static FragmentManager fragmentManager;//per i dialog
- static Node rootNode; //radice dell'albero corrente
- static ScrollView scroll; //Vertical ScrollView contenente l'albero
- static int nmoves=0;//numero di mosse
- static int nerrors=0;//nomero di errori
- static long startTime;//tempo di inizio dell'esercizio
- static RelativeLayout globalR; //layout globale contenente l'albero
- static int globalId =10; //variabile incrementale per l'assegnazione di ID univoci
- static DisplayMetrics v = new DisplayMetrics();//altezza lunghezza e densità dello schermo
-
- //Informazioni per la comunicazione client-server: username, password, nome dell'esercizio e chiave di sessione.
- static String user=null;
- static String pass=null;
- static String sessionKey=null;
- static String nomeEs=null;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- //routine standard per la create
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_my);
-
- //recupero e setto le variabili globali
- getWindowManager().getDefaultDisplay().getMetrics(v); //getMetrics() inserisce nella variabile V i dati metrici (altezza,larghezza,densità...) dello schermo del dispositivo
- globalHP = (RelativeLayout) findViewById(R.id.hpscroll);
- globalR = (RelativeLayout) findViewById(R.id.global);
- spaceSize = 2*(textSize/v.density); //lo spazio tra due sottoalberi è di due caratteri vuoti
- fragmentManager=getFragmentManager();
- copiedNode=null; //inizialmente non ci sono ovviamente sottoalberi copiati
- scroll=(ScrollView)findViewById(R.id.vscroll);
- startTime=time();
- b=new Border[1];
- b[0]=new Border(BorderedTextView.BORDER_TOP);
- b[0].setWidth(2);
- //Recupero i dati passati dall'Intent: username, password, nome dell'esercizio da visualizzare e chiave di sessione
- Bundle dati=getIntent().getExtras();
- nomeEs=dati.getString("nomeEs");
- user=dati.getString("user");
- pass=dati.getString("pass");
- sessionKey=dati.getString("sessionKey");
-
- //Creazione dell'albero: setto il rootNode e creo la Formula di partenza (F)
- if(!populateTree()) //populateTree() ha fallito: file dell'esercizio corrotto.
- return;
-
- //Creazione dell'albero: setto i campi della view
- BorderedTextView rootView=new BorderedTextView(this);
- rootView.setId(globalId);
- globalId++;
- selectedNode=null;
- addFakes=false; //di default non vengono aggiunte regole false
- rootNode.setView(rootView,globalR); //il rootNode viene legato al globalLayout attuale e alla TextView creata
- rootNode.handler=new touchnodeHandler(rootNode);
- rootNode.longHandler=new longnodeHandler(rootNode);
- rootNode.hasFocus=true; //di default, la radice ha inizialmente il focus.
- startDraw();
-
- //imposto il listener per il pinch zoom
- scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());
- }
- @Override
- public void onConfigurationChanged(Configuration configuration) //rotazione dello schermo: restore dell'albero
- {
- super.onConfigurationChanged(configuration);
- getWindowManager().getDefaultDisplay().getMetrics(v);
- DrawActivity.rootNode.Clean();
- DrawActivity.startDraw();
- }
- public static void startDraw()
- {
- RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- final int halfScreen=Math.round((v.widthPixels / 2) - rootNode.getBaseWidth() / 2); //calcolo la metà dello schermo per il calcolo dei margins della scrollView
- int leftSubTree=(Math.round(rootNode.getLeftOffset() - rootNode.getUpLine() / 2 + (rootNode.getBaseWidth() / 2)) + 40); //sottoalbero sinistro del rootNode più un padding di 40px
- lp.setMargins(Math.max(halfScreen,leftSubTree),0, 0, 0);
- lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, rootNode.view.getId());
- rootNode.view.setLayoutParams(lp);
- rootNode.view.setBorders(b);
- rootNode.view.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
- rootNode.view.setWidth(Math.round(rootNode.getLineWidth())); //setto la larghezza della view al contenuto della linea
- rootNode.view.setGravity(Gravity.CENTER); //formula al centro della sua overline
- rootNode.view.setText(rootNode.F.toString()); //setta il contenuto della formula
- globalR.addView(rootNode.view);
- //inserisco un listener a rootView da attivare quando sono state fissate le coordinate nel RelativeLayout
- rootNode.view.getViewTreeObserver().addOnGlobalLayoutListener(
- new ViewTreeObserver.OnGlobalLayoutListener()
- {
- @Override
- public void onGlobalLayout()
- {
- globalR.setPadding(0,0,(int)Math.max(rootNode.getRightOffset(),halfScreen),0);
- globalR.getLayoutParams().height=(int)Math.max((v.heightPixels),(rootNode.getMaxHeight()+rootNode.getMaxHeight()*0.10)); //altezza della view=max(h_screen,h_tree). Aggiungo un 10% all'altezza dell'albero per approssimazione dei calcoli.
- globalR.requestLayout(); //aggiorna i parametri e le dimensioni del RelativeLayout
- if (rootNode.getMaxHeight()<v.heightPixels/2) //se l'albero non ha raggiunto almeno la metà dello schermo scrollo la view fino in fondo per visualizzarlo interamente
- scroll.fullScroll(ScrollView.FOCUS_DOWN);
- rootNode.view.setOnClickListener(rootNode.handler);
- rootNode.view.setOnLongClickListener(rootNode.longHandler);
- rootNode.view.getViewTreeObserver().removeOnGlobalLayoutListener(this); //rimuove il listener per evitare che la funzione parta nuovamente
- rootNode.Draw();
- }
- });
- }
- @Override
- public void onBackPressed() {
- //abbandona esercizio e torna alla view con la lista degli esercizi
- Intent i = new Intent(globalHP.getContext(), aggiornamento.class);
- i.putExtra("user", user);
- i.putExtra("pass", pass);
- i.putExtra("sessionKey", sessionKey);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- globalHP.getContext().startActivity(i);
-
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.my, menu);
- return true;
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // TODO Auto-generated method stub
- scaleGestureDetector.onTouchEvent(event);
- return true;
- }
-
-
- @Override
- public boolean dispatchTouchEvent(MotionEvent event) {
- super.dispatchTouchEvent(event);
- return (scaleGestureDetector.onTouchEvent(event));
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- int id = item.getItemId();
- if(id==R.id.abandon)
- { //abbandona esercizio e torna alla view con la lista degli esercizi
- Intent i = new Intent(globalHP.getContext(), aggiornamento.class);
- i.putExtra("user", user);
- i.putExtra("pass", pass);
- i.putExtra("sessionKey", sessionKey);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- globalHP.getContext().startActivity(i);
- }
- return true;
- }
-
-
-
- public class simpleOnScaleGestureListener extends
- ScaleGestureDetector.SimpleOnScaleGestureListener {
-
- @Override
- public boolean onScale(ScaleGestureDetector detector) {
- float factor=detector.getScaleFactor();
- if (textSize>65 && factor>1) //font troppo grande: impedisco aumento
- return true;
- if (textSize<15 && factor<1) //font troppo piccolo: impedisco riduzione
- return true;
- rootNode.Refactor(); //risetta i campi dell'albero a FIELD_UNSET
- RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- DrawActivity.spaceSize=(DrawActivity.spaceSize*factor);
- DrawActivity.textSize=(DrawActivity.textSize*factor);
- int halfScreen=Math.round((v.widthPixels / 2)-rootNode.getBaseWidth()/2);
- int leftSubTree=(Math.round(rootNode.getLeftOffset() - rootNode.getUpLine() / 2 + (rootNode.getBaseWidth() / 2)) + 40);
- globalR.setPadding(0,0,(int)Math.max(rootNode.getRightOffset(),halfScreen),0);
- lp.setMargins(Math.max(halfScreen,leftSubTree),0, 0, 0);
- lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, rootNode.view.getId());
- rootNode.view.setLayoutParams(lp);
- rootNode.view.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
- rootNode.view.setWidth(Math.round(rootNode.getLineWidth())); //setto la larghezza della view al contenuto della linea
- rootNode.Resize(); //ricorsivamente sistema i fontsize, le larghezze e le posizioni delle view nell'albero
- return true;
- }
- }
- public static void finishedTree(Context context) {
- if (!rootNode.isCorrect()) {
- Toast.makeText(context, "Esercizio errato! Ricontrolla i passaggi fatti e riprova", Toast.LENGTH_LONG).show();
- } else {
- long endTime = time();
- int second= (int) ((endTime-startTime)/1000);
- int minute=second/60;
- Toast.makeText(context, "Esercizio completato in " + Integer.toString(nmoves) + " mosse!", Toast.LENGTH_LONG).show();
- try {
- sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- Toast.makeText(context, "Hai commesso " + Integer.toString(nerrors) + " errori!", Toast.LENGTH_LONG).show();
- try {
- sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- Toast.makeText(context, "Numero nodi dell'albero: " + Integer.toString(rootNode.count()), Toast.LENGTH_LONG).show();
- try {
- sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- Toast.makeText(context, "Tempo impiegato: " + Integer.toString(minute) + " minuti e " + Integer.toString(second-(minute*60)) + " secondi", Toast.LENGTH_LONG).show();
- //aggiungo l'esercizio al database
- int voto=valutazione.voto(nomeEs,nmoves,second,rootNode.count(),nerrors);
- personalDBHelper db = new personalDBHelper(context);
- db.add(user, nomeEs, MD5.digest(nomeEs), 0.0, voto, 0);
- nmoves = 0;
- Intent i = new Intent(context, aggiornamento.class);
- i.putExtra("user", user);
- i.putExtra("pass", pass);
- i.putExtra("sessionKey", sessionKey);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(i);
- }
- }
- public boolean populateTree() {
-
- rootNode = parser.root(nomeEs);
- if(rootNode==null)
- {
- Toast.makeText(this, "Errore nell'apertura del file", Toast.LENGTH_LONG).show();
- Intent i = new Intent(this, aggiornamento.class);
- i.putExtra("user", user);
- i.putExtra("pass", pass);
- i.putExtra("sessionKey", sessionKey);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- this.startActivity(i);
- return false;
- }
- for (Hypothesis h:rootNode.NodeHP)
- h.fromNode=rootNode;
- return true;
- }
- public static long time()
- {
- java.util.Date date= new java.util.Date();
- Timestamp now=new Timestamp(date.getTime());
- return now.getTime();
- }
-
-}
-