Android Open Source - joanne Welcome Fragment






From Project

Back to project page joanne.

License

The source code is released under:

GNU General Public License

If you think the Android project joanne 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 com.gmail.altakey.joanne.fragment;
/*from   w  ww. ja v a 2  s  .c o m*/
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.altakey.joanne.R;
import com.gmail.altakey.joanne.service.TweetBroadcastService;
import com.gmail.altakey.joanne.service.TwitterAuthService;

public class WelcomeFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context c, final Intent intent) {
            final String action = intent.getAction();
            if (TwitterAuthService.ACTION_AUTH_SUCCESS.equals(action)) {
                final Intent serviceLaunchIntent = new Intent(c, TweetBroadcastService.class);
                serviceLaunchIntent.setAction(TweetBroadcastService.ACTION_START);
                serviceLaunchIntent.putExtra(TweetBroadcastService.EXTRA_TOKEN, intent.getSerializableExtra(TwitterAuthService.EXTRA_TOKEN));
                c.startService(serviceLaunchIntent);
            } else if (TwitterAuthService.ACTION_AUTH_FAIL.equals(action)) {
                hideProcessingDialog();
                Toast.makeText(c, c.getString(R.string.auth_failure), Toast.LENGTH_LONG).show();
            } else if (TweetBroadcastService.ACTION_STATE_CHANGED.equals(action)) {
                hideProcessingDialog();
                updateTitle(getView());

                final Activity activity = getActivity();
                if (activity != null) {
                    activity.finish();
                }
            }
        }
    };

    public static WelcomeFragment newInstance(final int sectionNumber) {
        WelcomeFragment fragment = new WelcomeFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.fragment_welcome, container, false);
        final TextView proceed = (TextView)v.findViewById(R.id.auth);
        updateTitle(v);
        proceed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                final Context c = getActivity();
                if (c != null) {
                    showProcessingDialog();
                    if (TweetBroadcastService.sActive) {
                        TweetBroadcastService.requestQuit(c);
                    } else {
                        final Intent intent = new Intent(c, TwitterAuthService.class);
                        intent.setAction(TwitterAuthService.ACTION_AUTH);
                        c.startService(intent);
                    }
                }
            }
        });
        return v;
    }

    private void updateTitle(final View root) {
        final TextView proceed = (TextView)root.findViewById(R.id.auth);
        if (TweetBroadcastService.sActive) {
            proceed.setText(getString(R.string.stop));
        } else {
            proceed.setText(getString(R.string.start));
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        final IntentFilter filter = new IntentFilter();
        filter.addAction(TwitterAuthService.ACTION_AUTH_SUCCESS);
        filter.addAction(TwitterAuthService.ACTION_AUTH_FAIL);
        filter.addAction(TweetBroadcastService.ACTION_STATE_CHANGED);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, filter);

        updateTitle(getView());

        if (!TweetBroadcastService.sActive) {
            hideProcessingDialog();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
    }

    private void showProcessingDialog() {
        final FragmentActivity activity = getActivity();
        if (activity != null) {
            final DialogFragment dialog = ProcessingDialog.newInstance();
            dialog.show(activity.getSupportFragmentManager(), ProcessingDialog.TAG);
        }
    }

    private void hideProcessingDialog() {
        final FragmentActivity activity = getActivity();
        if (activity != null) {
            final DialogFragment dialog = (DialogFragment)activity.getSupportFragmentManager().findFragmentByTag(ProcessingDialog.TAG);
            if (dialog != null) {
                dialog.dismissAllowingStateLoss();
            }
        }
    }
}




Java Source Code List

com.gmail.altakey.joanne.activity.MainActivity.java
com.gmail.altakey.joanne.activity.SettingsActivity.java
com.gmail.altakey.joanne.fragment.ProcessingDialog.java
com.gmail.altakey.joanne.fragment.WelcomeFragment.java
com.gmail.altakey.joanne.hack.ToastAnimationCanceler.java
com.gmail.altakey.joanne.service.TweetBroadcastService.java
com.gmail.altakey.joanne.service.TwitterAuthService.java
com.gmail.altakey.joanne.util.IdListCoder.java
com.gmail.altakey.joanne.util.UserRelation.java
com.gmail.altakey.joanne.view.OutlinedTextView.java
com.gmail.altakey.joanne.view.RadioProfile.java
com.gmail.altakey.joanne.view.Radio.java
com.gmail.altakey.joanne.view.TweetDisplayBuilder.java
com.gmail.altakey.joanne.view.TweetView.java