Android Open Source - location-sharing-android Main Activity






From Project

Back to project page location-sharing-android.

License

The source code is released under:

MIT License

If you think the Android project location-sharing-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package lv.lu.locationsharing;
/*w  ww .  j av  a  2 s  .co m*/
import lv.lu.locationsharing.application.LocationApplication;
import lv.lu.locationsharing.config.Config;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.littlefluffytoys.littlefluffylocationlibrary.LocationLibrary;
import com.octo.android.robospice.JacksonSpringAndroidSpiceService;
import com.octo.android.robospice.SpiceManager;

public class MainActivity extends SherlockFragmentActivity {

  public static final Object GET_FRIENDS_CACHE_KEY = "getFriendsCacheKey";
  // Declare Variables
  DrawerLayout mDrawerLayout;
  ListView mDrawerList;
  ActionBarDrawerToggle mDrawerToggle;
  MenuListAdapter mMenuAdapter;
  String[] title;
  String[] subtitle;
  int[] icon;
  Fragment fragment1 = new Fragment1();
  Fragment fragment2 = new Fragment2();
  Fragment fragment3 = new Fragment3();
  Fragment fragment4 = new Fragment4();
  Fragment fragment5 = new Fragment5();
  private CharSequence mDrawerTitle;
  private CharSequence mTitle;
  protected SpiceManager spiceManager;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from drawer_main.xml
    setContentView(R.layout.drawer_main);

    // Get the Title
    mTitle = mDrawerTitle = getTitle();

    // Generate title
    title = new String[] { "S?kums", "Draugu saraksts", "Uzaicin?t draugu",
        "Uzaicin?tie draugi", "Uzaicin?jumi","Iestat?jumi","Iziet" };

    // Generate subtitle
    subtitle = new String[] { "", "", "", "", "", "", "" };

    // Generate icon
    icon = new int[] { R.drawable.action_about, R.drawable.action_settings,
        R.drawable.friends_add, R.drawable.action_settings,
        R.drawable.collections_cloud,R.drawable.settings,R.drawable.action_logout  };

    // Locate DrawerLayout in drawer_main.xml
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    // Locate ListView in drawer_main.xml
    mDrawerList = (ListView) findViewById(R.id.listview_drawer);

    // Set a custom shadow that overlays the main content when the drawer
    // opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
        GravityCompat.START);

    // Pass string arrays to MenuListAdapter
    mMenuAdapter = new MenuListAdapter(MainActivity.this, title, subtitle,
        icon);

    // Set the MenuListAdapter to the ListView
    mDrawerList.setAdapter(mMenuAdapter);

    // Capture listview menu item click
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // Enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
        R.drawable.ic_drawer, R.string.drawer_open,
        R.string.drawer_close) {

      public void onDrawerClosed(View view) {
        // TODO Auto-generated method stub
        super.onDrawerClosed(view);
      }

      public void onDrawerOpened(View drawerView) {
        // TODO Auto-generated method stub
        // Set the title on the action when drawer open
        // getSupportActionBar().setTitle(mDrawerTitle);
        super.onDrawerOpened(drawerView);
      }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
      selectItem(0);
    }
    LocationLibrary.forceLocationUpdate(MainActivity.this);

    spiceManager = new SpiceManager(JacksonSpringAndroidSpiceService.class);
    
  }

  
  @Override
  protected void onStart() {
    super.onStart();
    spiceManager.start(this);
    
  }

  @Override
  protected void onStop() {
    spiceManager.shouldStop();
    super.onStop();
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {

      if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
        mDrawerLayout.closeDrawer(mDrawerList);
      } else {
        mDrawerLayout.openDrawer(mDrawerList);
      }
    }

    return super.onOptionsItemSelected(item);
  }

  // ListView click listener in the navigation drawer
  private class DrawerItemClickListener implements
      ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
      selectItem(position);
    }
  }

  private void selectItem(int position) {

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    // Locate Position
    switch (position) {
    // J?samaina fragmenti vietam lai ir 1 2 3
    case 0:
      ft.replace(R.id.content_frame, fragment1);
      break;
    case 1:
      ft.replace(R.id.content_frame, fragment2);
      break;
    case 2:
      ft.replace(R.id.content_frame, fragment3);
      break;
    case 3:
      ft.replace(R.id.content_frame, fragment4);
      break;
    case 4:
      ft.replace(R.id.content_frame, fragment5);
      break;
    case 5:
      //ft.replace(R.id.content_frame, fragment5);
      break;
    case 6:
      doLogout();
      break;
      
    }
    ft.commit();
    mDrawerList.setItemChecked(position, true);

    // Get the title followed by the position
    setTitle(title[position]);
    // Close drawer
    mDrawerLayout.closeDrawer(mDrawerList);
  }

  private void doLogout() {
    LocationApplication mApp=(LocationApplication)getApplication();
    mApp.setConfig(new Config());
    Intent i = new Intent(getBaseContext(), LoginActivity.class);
    startActivity(i);
    finish();
  }
//    spiceManager.execute(new PostLogout(this,
//        ((LocationApplication)getApplication()).getConfig().getUserToken()), "",
//
//        DurationInMillis.ALWAYS_EXPIRED, new AuthenticationListener());
//  }
//  // inner class of your spiced Activity
//  private class AuthenticationListener implements
//      RequestListener<AuthenticationStatus> {
//
//    @Override
//    public void onRequestFailure(SpiceException spiceException) {
//      
//    }
//
//    @Override
//    public void onRequestSuccess(AuthenticationStatus authentication) 
//      goToLogin();
//    }
//  }

  @Override
  protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggles
    mDrawerToggle.onConfigurationChanged(newConfig);
  }

  @Override
  public void setTitle(CharSequence title) {
    mTitle = title;
    getSupportActionBar().setTitle(mTitle);
  }
}




Java Source Code List

lv.lu.locationsharing.Fragment1.java
lv.lu.locationsharing.Fragment2.java
lv.lu.locationsharing.Fragment3.java
lv.lu.locationsharing.Fragment4.java
lv.lu.locationsharing.Fragment5.java
lv.lu.locationsharing.LocationBroadcastReceiver.java
lv.lu.locationsharing.LoginActivity.java
lv.lu.locationsharing.MainActivity.java
lv.lu.locationsharing.MenuListAdapter.java
lv.lu.locationsharing.SignUpActivity.java
lv.lu.locationsharing.application.LocationApplication.java
lv.lu.locationsharing.config.Config.java
lv.lu.locationsharing.listview.adapter.RequestAdapter.java
lv.lu.locationsharing.model.AuthenticationStatus.java
lv.lu.locationsharing.model.Friend.java
lv.lu.locationsharing.model.GetFriends.java
lv.lu.locationsharing.model.InviteFriends.java
lv.lu.locationsharing.model.LocationPostStatus.java
lv.lu.locationsharing.model.Registration.java
lv.lu.locationsharing.requests.authentication.AuthenticationRequest.java
lv.lu.locationsharing.requests.authentication.AuthenticationStatusRequest.java
lv.lu.locationsharing.requests.friends.ConfirmFriendRequest.java
lv.lu.locationsharing.requests.friends.GetFriendsRequest.java
lv.lu.locationsharing.requests.friends.InviteFriendsRequest.java
lv.lu.locationsharing.requests.friends.PostLogout.java
lv.lu.locationsharing.requests.location.PostLocationUpdate.java
lv.lu.locationsharing.requests.registration.RegistrationRequest.java
lv.lu.locationsharing.utils.Constants.java
lv.lu.locationsharing.utils.Installation.java
lv.lu.locationsharing.utils.Url.java
lv.lu.locationsharing.utils.Utils.java