Java tutorial
package com.projecty.sleepgroundbox.activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.res.Configuration; import android.graphics.Typeface; import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.TextView; import com.mocoplex.adlib.AdlibManager; import com.mocoplex.adlib.dlg.AdlibDialogAdListener; import com.projecty.sleepgroundbox.R; import com.projecty.sleepgroundbox.fragment.BjLogFragment; import com.projecty.sleepgroundbox.fragment.CommunityFragment; import com.projecty.sleepgroundbox.fragment.FavoriteFragment; import com.projecty.sleepgroundbox.fragment.PlayListFragment; import com.projecty.sleepgroundbox.fragment.RecommendFragment; import com.projecty.sleepgroundbox.fragment.SandboxNetworkFragment; import com.projecty.sleepgroundbox.fragment.SearchFragment; import com.projecty.sleepgroundbox.fragment.SearchResultFragment; import com.projecty.sleepgroundbox.fragment.SettingFragment; import com.projecty.sleepgroundbox.fragment.VideoFragment; import com.projecty.sleepgroundbox.model.UserProfile; import com.projecty.sleepgroundbox.util.Global; import java.util.ArrayList; import java.util.List; /** * 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 * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * 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. */ public class HomeActivity extends ActionBarActivity implements View.OnClickListener { private static final int HOME_LAYOUT = 0; private static final int PLAYLIST_LAYOUT = 1; private static final int FAVORITE_LAYOUT = 2; private static final int FEATURED_LAYOUT = 3; private static final int BJLOG_LAYOUT = 4; private static final int COMMUNITY_LAYOUT = 5; private static final int BJ1_LAYOUT = 6; private static final int BJ2_LAYOUT = 7; private static final int BJ3_LAYOUT = 8; private static final int BJ4_LAYOUT = 9; private static final int BJ5_LAYOUT = 10; private static final int BJ6_LAYOUT = 11; private static final int BJ7_LAYOUT = 12; private static final String SET = "set"; private static final String HOME = "home"; private static final String PLAYLIST = "playlist"; private static final String FAVORITE = "favorite"; private static final String RECOMMEND = "recommend"; private static final String DLOG = "dlog"; private static final String COMMUNITY = "community"; private static final String BJ1 = "bj1"; private static final String BJ2 = "bj2"; private static final String BJ3 = "bj3"; private static final String BJ4 = "bj4"; private static final String BJ5 = "bj5"; private static final String BJ6 = "bj6"; private static final String BJ7 = "bj7"; private Boolean dlogState = false; private AdlibManager _amanager; // . private String ADLIB_API_KEY = "5574c9080cf2d5747f1ccdc5"; private List<FrameLayout> layoutList = new ArrayList<FrameLayout>(); Toolbar toolbar; DrawerLayout dlDrawer; ActionBarDrawerToggle dtToggle; private MenuItem item; LinearLayout drawer; public UserProfile user; @Override public void onBackPressed() { int num = getSupportFragmentManager().getBackStackEntryCount(); if (num <= 1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { int[] colors = new int[] { 0xffffffff, 0xffa8a8a8, 0xff404040, 0xff404040, 0xffdfdfdf }; _amanager.showAdDialog("", "?", "App ? ? ?", colors, new AdlibDialogAdListener() { @Override public void onLeftClicked() { } @Override public void onRightClicked() { HomeActivity.this.finish(); } }); } else { AlertDialog dialog; dialog = new AlertDialog.Builder(this) // .setIcon(R.drawable.warning) .setMessage("?") .setPositiveButton("", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //dialog.dismiss(); finish(); } }).setNegativeButton("", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel(); } }).show(); } } else { FragmentManager.BackStackEntry topFr = getSupportFragmentManager().getBackStackEntryAt(num - 2); String name = topFr.getName(); backStack(name); getSupportFragmentManager().popBackStackImmediate(); } } private void checkBackStack() { int num = getSupportFragmentManager().getBackStackEntryCount(); while (num >= 2) { getSupportFragmentManager().popBackStackImmediate(); num--; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.drawer_main); toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle(""); dlDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); setSupportActionBar(toolbar); ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeButtonEnabled(true); dtToggle = new ActionBarDrawerToggle(this, dlDrawer, R.string.app_name, R.string.app_name); dlDrawer.setDrawerListener(dtToggle); drawer = (LinearLayout) findViewById(R.id.drawer); drawer.setOnClickListener(new DrawerItemClickListener()); setupTopButton(); setLayoutList(); setUserProfile(); // ? ? ? . _amanager = new AdlibManager(ADLIB_API_KEY); _amanager.onCreate(this); if (Global.YOUTUBE_API_KEY.startsWith("YOUR_API_KEY")) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setMessage( "Edit ApiKey.java and replace \"YOUR_API_KEY\" with your Applications Browser API Key") .setTitle("Missing API Key") .setNeutralButton("Ok, I got it!", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finish(); } }); AlertDialog dialog = builder.create(); dialog.show(); } else if (savedInstanceState == null) { String tag = HOME; getSupportFragmentManager().beginTransaction().add(R.id.container, new VideoFragment(), tag) .addToBackStack(tag).commit(); } setUP(); } private void setUserProfile() { TextView id = (TextView) findViewById(R.id.user_id); TextView email = (TextView) findViewById(R.id.user_eamil); user = UserProfile.getUser(); id.setText(user.getUserName()); email.setText(user.getUserEmail()); } private void setUP() { TextView userId = (TextView) findViewById(R.id.user_id); TextView bjName = (TextView) findViewById(R.id.bj_name); Typeface custom_font = Typeface.createFromAsset(getAssets(), "NotoSans.otf"); userId.setTypeface(custom_font); bjName.setTypeface(custom_font); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_home, menu); MenuItem searchMenuItem = menu.findItem(R.id.action_search); final SearchView searchView = (SearchView) searchMenuItem.getActionView(); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String s) { SearchResultFragment fragment = new SearchResultFragment(); fragment.setQuery(s); getSupportFragmentManager().beginTransaction().add(R.id.container, fragment, "SEARCH_RESULT") .commit(); return false; } @Override public boolean onQueryTextChange(String s) { return false; } }); searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { closeFragment(); searchView.onActionViewCollapsed(); } } }); searchView.setOnSearchClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getSupportFragmentManager().beginTransaction().add(R.id.container, new SearchFragment(), "SEARCH") .commit(); } }); searchView.setOnCloseListener(new SearchView.OnCloseListener() { @Override public boolean onClose() { closeFragment(); return false; } }); return true; } private void closeFragment() { Fragment search = getSupportFragmentManager().findFragmentByTag("SEARCH"); if (search != null) { getSupportFragmentManager().popBackStack(search.getId(), 0); } Fragment search_result = getSupportFragmentManager().findFragmentByTag("SEARCH_RESULT"); if (search_result != null) { getSupportFragmentManager().popBackStack(search_result.getId(), 0); } } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (dtToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); dtToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); dtToggle.onConfigurationChanged(newConfig); } private void setupTopButton() { List<Button> buttons = new ArrayList<Button>(); buttons.add((Button) findViewById(R.id.settingButton)); buttons.add((Button) findViewById(R.id.inviteButton)); // buttons.add((Button) findViewById(R.id.readButton)); buttons.add((Button) findViewById(R.id.homeButton)); buttons.add((Button) findViewById(R.id.favoriteButton)); buttons.add((Button) findViewById(R.id.featuredButton)); buttons.add((Button) findViewById(R.id.bjlogButton)); buttons.add((Button) findViewById(R.id.communityButton)); buttons.add((Button) findViewById(R.id.playlistButton)); buttons.add((Button) findViewById(R.id.bj1)); buttons.add((Button) findViewById(R.id.bj2)); buttons.add((Button) findViewById(R.id.bj3)); buttons.add((Button) findViewById(R.id.bj4)); buttons.add((Button) findViewById(R.id.bj5)); buttons.add((Button) findViewById(R.id.bj6)); buttons.add((Button) findViewById(R.id.bj7)); Typeface font = Typeface.createFromAsset(getAssets(), "NotoSans.otf"); for (Button btn : buttons) { btn.setOnClickListener(this); btn.setTypeface(font); } } @Override public void onClick(View v) { String tag; switch (v.getId()) { case R.id.settingButton: toolbar.setTitle(""); tag = SET; checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, new SettingFragment(), tag) .addToBackStack(tag).commit(); break; case R.id.inviteButton: Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "[ BOX ]\n" + "TV? ? ? ' BOX' ! ? ? ? ! https://play.google.com/store/apps/details?id=com.projecty.sleepgroundbox"); sendIntent.setType("text/plain"); startActivity(sendIntent); break; // case R.id.readButton: // Toast toast2 = Toast.makeText(getApplicationContext(), // ".", Toast.LENGTH_SHORT); // toast2.setGravity(Gravity.CENTER, 0, 0); // toast2.show(); // break; case R.id.homeButton: toolbar.setTitle(""); tag = HOME; setLayoutBackgroundColor(HOME_LAYOUT); getSupportFragmentManager().beginTransaction().replace(R.id.container, new VideoFragment(), tag) .commit(); break; case R.id.playlistButton: toolbar.setTitle("??"); tag = PLAYLIST; setLayoutBackgroundColor(PLAYLIST_LAYOUT); checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, new PlayListFragment(), tag) .addToBackStack(tag).commit(); break; case R.id.favoriteButton: toolbar.setTitle("?"); tag = FAVORITE; setLayoutBackgroundColor(FAVORITE_LAYOUT); checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, new FavoriteFragment(), tag) .addToBackStack(tag).commit(); break; case R.id.featuredButton: toolbar.setTitle("??"); tag = RECOMMEND; setLayoutBackgroundColor(FEATURED_LAYOUT); checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, new RecommendFragment(), tag) .addToBackStack(tag).commit(); break; case R.id.bjlogButton: tag = DLOG; toolbar.setTitle(""); setLayoutBackgroundColor(BJLOG_LAYOUT); checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, new BjLogFragment(), tag) .addToBackStack(tag).commit(); break; case R.id.communityButton: tag = COMMUNITY; toolbar.setTitle(""); setLayoutBackgroundColor(COMMUNITY_LAYOUT); checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, new CommunityFragment(), tag) .addToBackStack(tag).commit(); break; case R.id.bj1: toolbar.setTitle("? BOX"); tag = BJ1; setLayoutBackgroundColor(BJ1_LAYOUT); SandboxNetworkFragment fr1 = new SandboxNetworkFragment(); fr1.setSandboxId("UUhQ-VMvdGrYZxviQVMTJOHg"); fr1.setSandboxCh("UChQ-VMvdGrYZxviQVMTJOHg"); checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, fr1, tag).addToBackStack(tag) .commit(); break; case R.id.bj2: tag = BJ2; toolbar.setTitle("? BOX"); setLayoutBackgroundColor(BJ2_LAYOUT); SandboxNetworkFragment fr2 = new SandboxNetworkFragment(); fr2.setSandboxId("UUtCnnCUn9IDDQRU9_04JD3g"); fr2.setSandboxCh("UCtCnnCUn9IDDQRU9_04JD3g"); checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, fr2, tag).addToBackStack(tag) .commit(); break; case R.id.bj3: tag = BJ3; toolbar.setTitle(" BOX"); setLayoutBackgroundColor(BJ3_LAYOUT); SandboxNetworkFragment fr3 = new SandboxNetworkFragment(); fr3.setSandboxId("UUEPuItFWOOJ2o5hTu65NlEg"); fr3.setSandboxCh("UCEPuItFWOOJ2o5hTu65NlEg"); checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, fr3, tag).addToBackStack(tag) .commit(); break; case R.id.bj4: tag = BJ4; toolbar.setTitle(" BOX"); setLayoutBackgroundColor(BJ4_LAYOUT); SandboxNetworkFragment fr4 = new SandboxNetworkFragment(); fr4.setSandboxId("UUxmBxNybpaLO7x61dm0oD8w"); fr4.setSandboxCh("UCxmBxNybpaLO7x61dm0oD8w"); checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, fr4, tag).addToBackStack(tag) .commit(); break; case R.id.bj5: tag = BJ5; toolbar.setTitle(" BOX"); setLayoutBackgroundColor(BJ5_LAYOUT); SandboxNetworkFragment fr5 = new SandboxNetworkFragment(); fr5.setSandboxId("UUT_Sf9z6Cqy11VHOfbnQPNQ"); fr5.setSandboxCh("UCT_Sf9z6Cqy11VHOfbnQPNQ"); checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, fr5, tag).addToBackStack(tag) .commit(); break; case R.id.bj6: tag = BJ6; toolbar.setTitle(" BOX"); setLayoutBackgroundColor(BJ6_LAYOUT); SandboxNetworkFragment fr6 = new SandboxNetworkFragment(); fr6.setSandboxId("UUiwOunGuqfKjcLIBsteAAJQ"); fr6.setSandboxCh("UCiwOunGuqfKjcLIBsteAAJQ"); checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, fr6, tag).addToBackStack(tag) .commit(); break; case R.id.bj7: tag = BJ7; toolbar.setTitle("? BOX"); setLayoutBackgroundColor(BJ7_LAYOUT); SandboxNetworkFragment fr7 = new SandboxNetworkFragment(); fr7.setSandboxId("UUt51IEo3ZxxOysVAG_ylR6w"); fr7.setSandboxCh("UCt51IEo3ZxxOysVAG_ylR6w"); checkBackStack(); getSupportFragmentManager().beginTransaction().replace(R.id.container, fr7, tag).addToBackStack(tag) .commit(); break; } dlDrawer.closeDrawer(drawer); } private void setLayoutBackgroundColor(int layout_id) { for (FrameLayout layout : layoutList) { layout.setBackgroundResource(R.color.drawer_background); } layoutList.get(layout_id).setBackgroundResource(R.color.theme_color2); } private void setLayoutList() { layoutList.add((FrameLayout) findViewById(R.id.home_layout)); layoutList.add((FrameLayout) findViewById(R.id.playlist_layout)); layoutList.add((FrameLayout) findViewById(R.id.favorite_layout)); layoutList.add((FrameLayout) findViewById(R.id.featured_layout)); layoutList.add((FrameLayout) findViewById(R.id.bjlog_layout)); layoutList.add((FrameLayout) findViewById(R.id.community_layout)); layoutList.add((FrameLayout) findViewById(R.id.bj1_layout)); layoutList.add((FrameLayout) findViewById(R.id.bj2_layout)); layoutList.add((FrameLayout) findViewById(R.id.bj3_layout)); layoutList.add((FrameLayout) findViewById(R.id.bj4_layout)); layoutList.add((FrameLayout) findViewById(R.id.bj5_layout)); layoutList.add((FrameLayout) findViewById(R.id.bj6_layout)); layoutList.add((FrameLayout) findViewById(R.id.bj7_layout)); } /* Drawer ? ? (? ) */ private class DrawerItemClickListener implements View.OnClickListener { @Override public void onClick(View v) { } } public void backStack(String fr) { switch (fr) { case SET: toolbar.setTitle(""); break; case HOME: toolbar.setTitle(""); setLayoutBackgroundColor(HOME_LAYOUT); break; case PLAYLIST: toolbar.setTitle("??"); setLayoutBackgroundColor(PLAYLIST_LAYOUT); break; case FAVORITE: toolbar.setTitle("?"); setLayoutBackgroundColor(FAVORITE_LAYOUT); break; case RECOMMEND: toolbar.setTitle("??"); setLayoutBackgroundColor(FEATURED_LAYOUT); break; case DLOG: toolbar.setTitle(""); setLayoutBackgroundColor(BJLOG_LAYOUT); break; case COMMUNITY: toolbar.setTitle(""); setLayoutBackgroundColor(COMMUNITY_LAYOUT); break; case BJ1: toolbar.setTitle("? BOX"); setLayoutBackgroundColor(BJ1_LAYOUT); break; case BJ2: toolbar.setTitle("? BOX"); setLayoutBackgroundColor(BJ2_LAYOUT); break; case BJ3: toolbar.setTitle(" BOX"); setLayoutBackgroundColor(BJ3_LAYOUT); break; case BJ4: toolbar.setTitle(" BOX"); setLayoutBackgroundColor(BJ4_LAYOUT); break; case BJ5: toolbar.setTitle(" BOX"); setLayoutBackgroundColor(BJ5_LAYOUT); break; case BJ6: toolbar.setTitle(" BOX"); setLayoutBackgroundColor(BJ6_LAYOUT); break; case BJ7: toolbar.setTitle("? BOX"); setLayoutBackgroundColor(BJ7_LAYOUT); break; default: break; } } }