/**
* Copyright (C) 2015 Monitordroid Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Tyler Butler
**/
package com.monitordroid.app;
import static com.monitordroid.app.CommonUtilities.DISPLAY_MESSAGE_ACTION;
import static com.monitordroid.app.CommonUtilities.EXTRA_MESSAGE;
import static com.monitordroid.app.CommonUtilities.SENDER_ID;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.gcm.GCMRegistrar;
public class MainActivity extends Activity {
TextView lblMessage;
AsyncTask<Void, Void, Void> mRegisterTask;
AlertDialogManager alert = new AlertDialogManager();
// Shared preference to save account name and devicename for later access
public static final String PREFS_NAME = "MyPrefsFile";
ConnectionDetector cd;
public static String name;
public static String email;
/**
* This activity is created when the "Register" button is clicked from
* RegisterActivity.java
*
* Handles registering for a GCM ID, internet connectivity issues, and calls
* the method to register the device on the Monitordroid server
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cd = new ConnectionDetector(getApplicationContext());
// Check if able to connect to the internet
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
return;
}
// Getting name, email from intent
Intent i = getIntent();
// Here are the "Unique Device Name" and "Account Email" fields input by
// the user on the screen before
name = i.getStringExtra("name");
email = i.getStringExtra("email");
// Make sure the device has the proper dependencies
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
lblMessage = (TextView) findViewById(R.id.lblMessage);
registerReceiver(mHandleMessageReceiver, new IntentFilter(
DISPLAY_MESSAGE_ACTION));
// Get GCM registration id
final String regId = GCMRegistrar.getRegistrationId(this);
// Check if regid already present
if (regId.equals("")) {
// Registration is not present, register now with GCM
GCMRegistrar.register(this, SENDER_ID);
// Save the device's username and devicename for later access
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", email);
editor.putString("devicename", name);
editor.commit();
}
else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// Register device on Monitordroid server
ServerUtilities.register(context, name, email, regId);
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
GCMHeartbeatAlarm gcmAlarm = new GCMHeartbeatAlarm();
gcmAlarm.setAlarm(this);
}
/**
* Broadcast receiver for receiving GCM messages
* */
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
lblMessage.append(newMessage + "\n");
}
};
@Override
protected void onDestroy() {
super.onStop();
if (mRegisterTask != null) {
mRegisterTask.cancel(true);
}
try {
unregisterReceiver(mHandleMessageReceiver);
GCMRegistrar.onDestroy(this);
}
catch (Exception e) {
}
super.onDestroy();
}
}