Skip to content

Commit

Permalink
Load service after the location is retrieved.
Browse files Browse the repository at this point in the history
  • Loading branch information
irinil committed Aug 12, 2020
1 parent 31e1c9d commit 9c37211
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 108 deletions.
14 changes: 3 additions & 11 deletions src/main/java/de/tudarmstadt/informatik/hostage/Hostage.java
Expand Up @@ -6,8 +6,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
Expand Down Expand Up @@ -318,29 +316,23 @@ public IBinder onBind(Intent intent) {
public void onCreate() {
super.onCreate();
context = new WeakReference<>(getApplicationContext());
loadProtocols();
loadConnectionInfoEditor();
loadProtocols();
mProtocolActiveAttacks = new HashMap<>();
createNotification();
registerNetReceiver();
loadConnectionInfo();
executeRoot();
loadConnectionInfo();
}

private void loadConnectionInfo(){
try {
TimeUnit.MILLISECONDS.sleep(70);
updateConnectionInfo();
} catch (InterruptedException e) {
e.printStackTrace();
}
updateConnectionInfo();
}

private void loadConnectionInfoEditor(){
connectionInfo = getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
connectionInfoEditor = connectionInfo.edit();
connectionInfoEditor.apply();

}

private void loadProtocols(){
Expand Down
Expand Up @@ -93,8 +93,7 @@ public MyLocationManager(Context context) {

return;
}
newestLocation = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
newestLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

}

Expand Down Expand Up @@ -128,18 +127,15 @@ public void getUpdates(long time, int attempts, Context context) {
public void startUpdates(Context context) {
boolean gpsEnabled = false;
boolean networkEnabled = false;
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// exceptions will be thrown if provider is not permitted.
try {
gpsEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
ex.printStackTrace();
}
try {
networkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
ex.printStackTrace();
}
Expand All @@ -160,11 +156,9 @@ private void requestLocationUpdates(boolean networkEnabled,Context context) {

return;
}
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
if (networkEnabled)
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

}

Expand All @@ -187,12 +181,9 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
dialog.setTitle("Permission Required");
dialog.setCancelable(false);
dialog.setMessage("You have to Allow permission to access user location");
dialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package",
context.getPackageName(), null));
}
dialog.setPositiveButton("Settings", (dialog1, which) -> {
Intent i = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package",
context.getPackageName(), null));
});
AlertDialog alertDialog = dialog.create();
alertDialog.show();
Expand Down
Expand Up @@ -159,11 +159,6 @@ public void onServiceDisconnected(ComponentName name) {

};

/**
* Holds an profile manager instance
*/
private ProfileManager mProfileManager;

/**
* Holds the root fragment for our hierarchical fragment navigation
*/
Expand Down Expand Up @@ -265,7 +260,6 @@ protected void onCreate(Bundle savedInstanceState) {
// sets the static context reference to the application context
context = new WeakReference<>(getApplicationContext());
setContentView(R.layout.activity_drawer_main);
getLocationData();

// init threat indicator animation
ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING);
Expand All @@ -276,69 +270,17 @@ protected void onCreate(Bundle savedInstanceState) {
arr.recycle();

// configures the action bar
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);

// sets the drawer and action title to the application title
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerList = findViewById(R.id.left_drawer);

// propagates the navigation drawer with items
mDrawerItems = new ArrayList<>();
mDrawerItems.add(new DrawerListItem(R.string.drawer_overview, R.drawable.ic_menu_home));
mDrawerItems.add(new DrawerListItem(R.string.drawer_threat_map, R.drawable.ic_menu_mapmode));
mDrawerItems.add(new DrawerListItem(R.string.drawer_records, R.drawable.ic_menu_records));
mDrawerItems.add(new DrawerListItem(R.string.drawer_statistics, R.drawable.ic_menu_stats));
mDrawerItems.add(new DrawerListItem(R.string.drawer_services, R.drawable.ic_menu_set_as));
mDrawerItems.add(new DrawerListItem(R.string.drawer_profile_manager, R.drawable.ic_menu_allfriends));
mDrawerItems.add(new DrawerListItem(R.string.drawer_settings, R.drawable.ic_menu_preferences));
mDrawerItems.add(new DrawerListItem(R.string.drawer_app_info, R.drawable.ic_menu_info_details));
configureActionBar();

DrawerListAdapter listAdapter = new DrawerListAdapter(this, mDrawerItems);

mDrawerList.setAdapter(listAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

// configures the navigation drawer
mDrawerToggle = new androidx.appcompat.app.ActionBarDrawerToggle(this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}

public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};

mDrawerLayout.setDrawerListener(mDrawerToggle);
// start the hostage service
startAndBind();
loadDrawer();

// ActivityCompat.requestPermissions(MainActivity.this,
// new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
// MY_PERMISSIONS_REQUEST_WRITE_STORAGE);

mSharedPreferences = getSharedPreferences(getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE);

if(mSharedPreferences.getBoolean("isFirstRun", true)){

// opens navigation drawer if first run
mDrawerLayout.postDelayed(() -> mDrawerLayout.openDrawer(Gravity.LEFT), 1000);

onFirstRun();
}
getLocationData();
loadFirstRun();
//Must start after the location!
startAndBind();

if (savedInstanceState == null) {
// on first time display view for first nav item
Expand Down Expand Up @@ -374,21 +316,17 @@ private void onFirstRun(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(Html.fromHtml(getString(R.string.hostage_disclaimer)))
.setCancelable(false)
.setPositiveButton(getString(R.string.agree), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// and, if the user accept, you can execute something like this:
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean("isFirstRun", false);
// Commit the edits!
editor.apply();

// Enabled shared preferences for 'first' time non-portbinder activation
SharedPreferences.Editor editor1= mSharedPreferences.edit();
editor1.putBoolean("isFirstEmulation", true);
editor1.apply();
}
.setPositiveButton(getString(R.string.agree), (dialog, id) -> {
// and, if the user accept, you can execute something like this:
// We need an Editor object to make preference changes.
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean("isFirstRun", false);
editor.apply();

// Enabled shared preferences for 'first' time non-portbinder activation
SharedPreferences.Editor editor1= mSharedPreferences.edit();
editor1.putBoolean("isFirstEmulation", true);
editor1.apply();
})
.setNegativeButton(getString(R.string.disagree), (dialog, id) -> {
getHostageService().stopListeners();
Expand All @@ -399,6 +337,76 @@ public void onClick(DialogInterface dialog, int id) {
alert.show();
}

private void loadFirstRun(){
mSharedPreferences = getSharedPreferences(getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE);
if(mSharedPreferences.getBoolean("isFirstRun", true)){
// opens navigation drawer if first run
mDrawerLayout.postDelayed(() -> mDrawerLayout.openDrawer(Gravity.LEFT), 1000);

onFirstRun();
}

}

private void configureActionBar(){
ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}

private void loadDrawer(){
// sets the drawer and action title to the application title
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerList = findViewById(R.id.left_drawer);

// propagates the navigation drawer with items
mDrawerItems = new ArrayList<>();
mDrawerItems.add(new DrawerListItem(R.string.drawer_overview, R.drawable.ic_menu_home));
mDrawerItems.add(new DrawerListItem(R.string.drawer_threat_map, R.drawable.ic_menu_mapmode));
mDrawerItems.add(new DrawerListItem(R.string.drawer_records, R.drawable.ic_menu_records));
mDrawerItems.add(new DrawerListItem(R.string.drawer_statistics, R.drawable.ic_menu_stats));
mDrawerItems.add(new DrawerListItem(R.string.drawer_services, R.drawable.ic_menu_set_as));
mDrawerItems.add(new DrawerListItem(R.string.drawer_profile_manager, R.drawable.ic_menu_allfriends));
mDrawerItems.add(new DrawerListItem(R.string.drawer_settings, R.drawable.ic_menu_preferences));
mDrawerItems.add(new DrawerListItem(R.string.drawer_app_info, R.drawable.ic_menu_info_details));

DrawerListAdapter listAdapter = new DrawerListAdapter(this, mDrawerItems);

mDrawerList.setAdapter(listAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

setmDrawerToggle();

}

private void setmDrawerToggle(){
// configures the navigation drawer
mDrawerToggle = new androidx.appcompat.app.ActionBarDrawerToggle(this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}

public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};

mDrawerLayout.setDrawerListener(mDrawerToggle);

}

/**
* Starts an Instance of MyLocationManager to set the hostage.location within this
* class.
Expand Down

0 comments on commit 9c37211

Please sign in to comment.