Android Open Source - Multi-Mania-app Main Activity






From Project

Back to project page Multi-Mania-app.

License

The source code is released under:

MIT License

If you think the Android project Multi-Mania-app 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 be.ana.nmct.multimania.ui;
//from  w  w w  .  j a v  a 2s  .  c  o m
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.common.AccountPicker;

import be.ana.nmct.multimania.R;
import be.ana.nmct.multimania.data.MultimaniaContract;
import be.ana.nmct.multimania.utils.SettingsUtil;
import be.ana.nmct.multimania.utils.Utility;


public class MainActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    private static final String TAG = MainActivity.class.getSimpleName();

    public static final String PREFERENCE_NAME = "launch_values";
    public static final String PREFERENCE_FIRST_TIME_LAUNCH = "first_time_launch";
    public static final String PREFERENCE_ACCOUNT = "account_name";

    public static final int LOADER_SCHEDULE_DATES_ID    = 0;
    public static final int LOADER_SCHEDULE_TALK_ID     = 10;
    public static final int LOADER_MY_SCHEDULE_TALK_ID  = 20;
    public static final int LOADER_NEWS_ID              = 3;
    public static final int LOADER_SETTINGS             = 5;
    public static final int LOADER_SUGGESTIONS          = 15;

    // The authority for the sync adapter's content provider
    public static final String AUTHORITY = MultimaniaContract.CONTENT_AUTHORITY;
    // An account type, in the form of a domain name
    public static final String ACCOUNT_TYPE = "multi-mania.be";
    // The account name
    public static final String ACCOUNT = "Mult-Mania";
    // Instance fields
    Account mAccount;

    private MenuItem mAddAccountMenuItem;

    // Sync interval constants
    public static final long SYNC_INTERVAL = 60L*60L*24;

    private NavigationDrawerFragment mNavigationDrawerFragment;
    private SettingsUtil mAccountSettings;
    private CharSequence mTitle;
    private Boolean mFirstTimeLaunch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Handle first time launching
        SettingsUtil launchUtil = new SettingsUtil(this, PREFERENCE_NAME);
        mFirstTimeLaunch = launchUtil.getBooleanPreference(PREFERENCE_FIRST_TIME_LAUNCH, true);

        if(mFirstTimeLaunch){
            super.onResume();
            Intent intent = new Intent(this,LoadActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(intent);
            //return;
        }

        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();
        Configuration configuration = getResources().getConfiguration();
        if ((configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL||
            (configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL||
            (configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE){
            // Set up the drawer.
            mNavigationDrawerFragment.setUp(
                    R.id.navigation_drawer,
                    (DrawerLayout) findViewById(R.id.drawer_layout));
        }

        mAccount = CreateSyncAccount(this);
        mAccountSettings = new SettingsUtil(getApplicationContext(), PREFERENCE_NAME);

        //Beautify for Lollipop users
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(getResources().getColor(R.color.primaryColorDark));
        }

        ContentResolver.addPeriodicSync(
                mAccount,
                AUTHORITY,
                Bundle.EMPTY,
                SYNC_INTERVAL);
    }

    @Override
    protected void onResume() {
        super.onResume();

        //set default settings
        if(mFirstTimeLaunch){
            SettingsUtil settingsUtil = new SettingsUtil(this, SettingsFragment.PREFERENCE_NAME);
            settingsUtil.setPreference(SettingsFragment.PREFERENCE_NOTIFY, true);
            settingsUtil.setPreference(SettingsFragment.PREFERENCE_SYNC, false);
        }

    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.container);
        //boolean isNull = fragment==null;
        boolean isNotRetained = true;
        switch (position) {
            case 0:
                isNotRetained=!(fragment instanceof SchedulesFragment);
                if(isNotRetained) {
                    fragment = new SchedulesFragment();
                }
                break;
            case 1:
                isNotRetained=!(fragment instanceof MySchedulesFragment);
                if(isNotRetained) {
                    fragment = new MySchedulesFragment();
                }
                break;
            case 2:
                fragment = new MapFragment();
                break;
            case 3:
                isNotRetained=!(fragment instanceof NewsFragment);
                if(isNotRetained){
                    fragment = new NewsFragment();
                }
                break;
            case 4:
                fragment = new AboutFragment();
                break;
            case 5:
                fragment = new SettingsFragment();
                break;
        }
        if(isNotRetained){
            FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.setCustomAnimations(R.anim.fade_in_object,R.anim.fade_out_object);
            ft.replace(R.id.container,fragment).commit();
        }
        onSectionAttached(position);
    }
    public void onSectionAttached(int number) {

        switch (number) {
            case 0:
                mTitle = getString(R.string.title_schedule);
                break;
            case 1:
                mTitle = getString(R.string.title_myschedule);
                break;
            case 2:
                mTitle = getString(R.string.title_map);
                break;
            case 3:
                mTitle = getString(R.string.title_news);
                break;
            case 4:
                mTitle = getString(R.string.title_about);
                break;
            case 5:
                mTitle = getString(R.string.title_settings);
                break;
            //add more cases here
        }
        getActionBar().setTitle(mTitle);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        mAddAccountMenuItem = menu.findItem(R.id.action_account);

        if(mAccountSettings.getStringPreference(PREFERENCE_ACCOUNT) == null){
            mAddAccountMenuItem.setIcon(R.drawable.ic_action_add_person);
        }

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()){
            case R.id.action_account:

                TextView txtDialogMessage = new TextView(this);
                txtDialogMessage.setGravity(Gravity.CENTER_HORIZONTAL);
                txtDialogMessage.setPadding(20, 10, 20, 15);
                txtDialogMessage.setTextAppearance(this,android.R.style.TextAppearance_Medium);

                AlertDialog.Builder builder = new AlertDialog.Builder(this);

                if(mAccountSettings.getStringPreference(PREFERENCE_ACCOUNT) == null){
                    txtDialogMessage.setText(R.string.login_description);
                    builder.setPositiveButton(R.string.dialog_confirm, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            askUserEmail();
                        }
                    });
                } else {
                    txtDialogMessage.setText(R.string.login_loggedin_description);
                    builder.setPositiveButton(R.string.login_button_other_email, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            askUserEmail();
                        }
                    });
                }

                builder.setView(txtDialogMessage)
                        .setTitle(R.string.action_account)
                        .setCancelable(true)
                        .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    public void openTwitter(View v){
        launchUrlIntent(getString(R.string.twitter_url));
    }
    public void openFacebook(View v){
        launchUrlIntent(getString(R.string.facebook_url));
    }
    public void openSite(View v){
        launchUrlIntent(getString(R.string.site_url));
    }

    public void launchUrlIntent(String url){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);
    }

    /**
     * Create a new dummy account for the sync adapter
     *
     * @param context The application context
     */
    public static Account CreateSyncAccount(Context context) {
        // Create the account type and default account
        Account newAccount = new Account(
                ACCOUNT, ACCOUNT_TYPE);
        // Get an instance of the Android account manager
        AccountManager accountManager =
                (AccountManager) context.getSystemService(
                        ACCOUNT_SERVICE);
        /*
         * Add the account and account type, no password or user data
         * If successful, return the Account object, otherwise report an error.
         */
        if (accountManager.addAccountExplicitly(newAccount, null, null)) {
            /*
             * If you don't set android:syncable="true" in
             * in your <provider> element in the manifest,
             * then call context.setIsSyncable(account, AUTHORITY, 1)
             * here.
             */
        } else {
            /*
             * The account exists or some other error occurred. Log this, report it,
             * or handle it internally.
             */
        }
        return newAccount;
    }

    private void askUserEmail() {
        try {
            Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, false, null, null, null, null);
            startActivityForResult(intent, SettingsFragment.REQUEST_CODE_EMAIL);
        } catch (ActivityNotFoundException e) {
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SettingsFragment.REQUEST_CODE_EMAIL && data != null && resultCode == Activity.RESULT_OK) {

            String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            Log.d(TAG,"AccountName:"+accountName);
            if (!TextUtils.isEmpty(accountName)) {
                mAccountSettings.setPreference(PREFERENCE_ACCOUNT, accountName);
                Toast.makeText(this, getString(R.string.account_success), Toast.LENGTH_SHORT).show();
                Utility.requestSync(Utility.getSyncAccount(this),true);
                mAddAccountMenuItem.setIcon(R.drawable.ic_action_person);
            } else {
                Toast.makeText(this, getString(R.string.account_picker_invalid), Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(this, getString(R.string.account_picker_none), Toast.LENGTH_LONG).show();
        }
    }

}




Java Source Code List

be.ana.nmct.multimania.ApiTest.java
be.ana.nmct.multimania.ApplicationTest.java
be.ana.nmct.multimania.CalendarTest.java
be.ana.nmct.multimania.DbTest.java
be.ana.nmct.multimania.Import.java
be.ana.nmct.multimania.NotificationImport.java
be.ana.nmct.multimania.ProviderTest.java
be.ana.nmct.multimania.SyncTest.java
be.ana.nmct.multimania.data.ApiActions.java
be.ana.nmct.multimania.data.DbHelper.java
be.ana.nmct.multimania.data.GsonLoader.java
be.ana.nmct.multimania.data.MultimaniaContract.java
be.ana.nmct.multimania.data.MultimaniaProvider.java
be.ana.nmct.multimania.data.NewsItemLoader.java
be.ana.nmct.multimania.data.RoomLoader.java
be.ana.nmct.multimania.data.TagLoader.java
be.ana.nmct.multimania.data.TalkLoader.java
be.ana.nmct.multimania.model.IData.java
be.ana.nmct.multimania.model.NewsItem.java
be.ana.nmct.multimania.model.Room.java
be.ana.nmct.multimania.model.Speaker.java
be.ana.nmct.multimania.model.Tag.java
be.ana.nmct.multimania.model.TalkSpeaker.java
be.ana.nmct.multimania.model.TalkTag.java
be.ana.nmct.multimania.model.Talk.java
be.ana.nmct.multimania.model.User.java
be.ana.nmct.multimania.service.AuthenticatorService.java
be.ana.nmct.multimania.service.Authenticator.java
be.ana.nmct.multimania.service.BootListener.java
be.ana.nmct.multimania.service.NotificationReceiver.java
be.ana.nmct.multimania.service.NotificationSender.java
be.ana.nmct.multimania.service.SyncAdapter.java
be.ana.nmct.multimania.service.SyncService.java
be.ana.nmct.multimania.ui.AboutFragment.java
be.ana.nmct.multimania.ui.LoadActivity.java
be.ana.nmct.multimania.ui.MainActivity.java
be.ana.nmct.multimania.ui.MapFragment.java
be.ana.nmct.multimania.ui.MyScheduleFragment.java
be.ana.nmct.multimania.ui.MySchedulesFragment.java
be.ana.nmct.multimania.ui.NavigationDrawerFragment.java
be.ana.nmct.multimania.ui.NewsFragment.java
be.ana.nmct.multimania.ui.NewsItemActivity.java
be.ana.nmct.multimania.ui.NewsItemFragment.java
be.ana.nmct.multimania.ui.ScheduleFragment.java
be.ana.nmct.multimania.ui.SchedulesFragment.java
be.ana.nmct.multimania.ui.SettingsFragment.java
be.ana.nmct.multimania.ui.SuggestionFragment.java
be.ana.nmct.multimania.ui.TalkActivity.java
be.ana.nmct.multimania.ui.TalkFragment.java
be.ana.nmct.multimania.utils.GoogleCalUtil.java
be.ana.nmct.multimania.utils.SettingsHelper.java
be.ana.nmct.multimania.utils.SettingsUtil.java
be.ana.nmct.multimania.utils.SyncUtils.java
be.ana.nmct.multimania.utils.Utility.java
be.ana.nmct.multimania.vm.NavigationItem.java
be.ana.nmct.multimania.vm.ScheduleTalkVm.java
be.ana.nmct.multimania.widget.StaggeredGridView.java
com.bulletnoid.android.widget.StaggeredGridView.BulletStaggeredGridView.java
com.bulletnoid.android.widget.StaggeredGridView.HeaderFooterListAdapter.java
com.bulletnoid.android.widget.StaggeredGridView.ScrollerCompatIcs.java
com.bulletnoid.android.widget.StaggeredGridView.ScrollerCompat.java