Skip to content

Commit

Permalink
Fix bug #1 and #2
Browse files Browse the repository at this point in the history
  • Loading branch information
mariogrip committed Oct 30, 2014
1 parent 0853ee5 commit 7e94a0f
Show file tree
Hide file tree
Showing 6 changed files with 401 additions and 107 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".service"
android:exported="false"/>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
107 changes: 101 additions & 6 deletions app/src/main/java/com/mariogrip/octodroid/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@ public class Activity extends ActionBarActivity {
public static String jsonData_printer;
protected SharedPreferences prefs;
private get get_class;
protected String ip;
protected String key;
private boolean senderr = false;
protected static boolean running = false;
protected static boolean printing;
protected static boolean push = true;
protected static boolean servicerunning =false;
protected static String ip;
protected static String key;
private Timer timer = new Timer();
private TimerTask timerTask;
private Timer timer2 = new Timer();
private TimerTask timerTask2;
public static boolean server_status = false;
private static final int RESULT_SETTINGS = 1;



@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -41,13 +51,17 @@ protected void onCreate(Bundle savedInstanceState) {
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);


super.onCreate(savedInstanceState);
setContentView(R.layout.status_tab);
prefs = PreferenceManager.getDefaultSharedPreferences(Activity.this);
ip = prefs.getString("ip", "localhost");
key = prefs.getString("api", "0");
senderr = prefs.getBoolean("err", true);
push = prefs.getBoolean("push", true);
get_class = new get();
Log.d("OctoPrint","test");
running = false;
runner();

AlertDialog.Builder builder = new AlertDialog.Builder(this);
Expand All @@ -63,24 +77,71 @@ public void onClick(DialogInterface dialog, int id) {
Activity.this.finish();
}
});

AlertDialog dialog = builder.create();
dialog.show();
if (push) {
if (!servicerunning) {
servicerunning = true;
Intent mServiceIntent = new Intent(this, service.class);
this.startService(mServiceIntent);
}
}else{
if (servicerunning){
servicerunning = false;
Intent mServiceIntent = new Intent(this, service.class);
this.stopService(mServiceIntent);
}
}
}

public void servererr(){
timerTask2 = new TimerTask() {
@Override
public void run() {
get.refreshJson(ip, "job", key);
if (server_status){
runner();
timerTask2.cancel();
return;
}
}

};
timer2.schedule(timerTask2, 0, 10000);
}

public void logD(String e){
Log.d("OctoDroid",e);
}
public void runner(){
get.refreshJson(ip, "job", key);
if (running){
logD("Stopping runner, Might started twice");
return;
}
if (!running){
logD("OneRunStarted");
running = true;
}
timerTask = new TimerTask() {
@Override
public void run() {
if (!server_status){
logD("Server Error");
running = false;
servererr();
timerTask.cancel();
return;
}

Activity.this.runOnUiThread(new Runnable() {
public void run() {
get.refreshJson(ip, "job", key);
get.refreshJson(ip, "printer", key);

get.decodeJson();
logD("Running runner");
if (server_status) {
Log.d("test123",get.getData("job", "printTime"));
ProgressBar progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setProgress(get.getProgress());
TextView texttime = (TextView) findViewById(R.id.textView11_time);
TextView textpri = (TextView) findViewById(R.id.textView16_printed);
TextView textest = (TextView) findViewById(R.id.textView13_est);
Expand Down Expand Up @@ -112,6 +173,7 @@ public void run() {
textfila.setText(" " + "-");
texttimel.setText(" " + "-");
textprinttime.setText(" " + get.toHumanRead(Double.parseDouble(get.getData("job", "printTime").toString())));
progress.setProgress(get.getProgress());
}
}
});
Expand Down Expand Up @@ -153,9 +215,42 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
prefs = PreferenceManager.getDefaultSharedPreferences(Activity.this);
ip = prefs.getString("ip", "localhost");
key = prefs.getString("api", "0");
push = prefs.getBoolean("push", true);
if (push) {
if (!servicerunning) {
servicerunning = true;
Intent mServiceIntent = new Intent(this, service.class);
this.startService(mServiceIntent);
}
}else{
if (servicerunning){
servicerunning = false;
Intent mServiceIntent = new Intent(this, service.class);
this.stopService(mServiceIntent);
}
}
break;

}

}
public void onPause(){
super.onPause();
running = false;
timerTask.cancel();
}
public void onResume(){
super.onResume();
runner();
}
public void onStop(){
super.onStop();
running = false;
timerTask.cancel();
}
public void onStart(){
super.onStart();
runner();
}

}
Loading

0 comments on commit 7e94a0f

Please sign in to comment.