Java tutorial
/* * Created by Hanks * Copyright (c) 2015 NaShangBan. All rights reserved * * 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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. */ package app.hanks.com.conquer.activity; import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.os.Build; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.util.DisplayMetrics; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.ImageButton; import android.widget.TextView; import com.lidroid.xutils.DbUtils; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; import java.util.List; import app.hanks.com.conquer.CustomApplication; import app.hanks.com.conquer.R; import app.hanks.com.conquer.bean.User; import app.hanks.com.conquer.util.A; import app.hanks.com.conquer.util.CollectionUtils; import app.hanks.com.conquer.util.L; import app.hanks.com.conquer.util.SP; import app.hanks.com.conquer.util.SystemBarTintManager; import cn.bmob.im.BmobChatManager; import cn.bmob.im.BmobUserManager; import cn.bmob.im.bean.BmobChatUser; import cn.bmob.v3.listener.FindListener; import cn.bmob.v3.listener.UpdateListener; /** * Activity,??,??, * Created by Hanks on 2015/5/17. */ public abstract class BaseActivity extends FragmentActivity { protected Context context; protected BmobUserManager userManager; protected BmobChatManager manager; protected CustomApplication mApplication; protected User currentUser; protected int mScreenWidth; protected int mScreenHeight; protected ImageLoader loader; protected DisplayImageOptions option_photo, option_pic; DbUtils dbUtils; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = this; initStatusBar(); initTheme(); initLayout(); userManager = BmobUserManager.getInstance(getApplicationContext()); manager = BmobChatManager.getInstance(getApplicationContext()); mApplication = CustomApplication.getInstance(); currentUser = BmobChatUser.getCurrentUser(context, User.class); DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); mScreenWidth = metric.widthPixels; mScreenHeight = metric.heightPixels; loader = ImageLoader.getInstance(); option_photo = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.default_photo) .showImageForEmptyUri(R.drawable.default_photo).showImageOnFail(R.drawable.default_photo) .cacheInMemory(true).cacheOnDisk(true).considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565) .displayer(new FadeInBitmapDisplayer(200)).build(); option_pic = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.pic_loading) .showImageForEmptyUri(R.drawable.pic_loading).showImageOnFail(R.drawable.pic_loading) .cacheInMemory(true).cacheOnDisk(true).considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565) .displayer(new FadeInBitmapDisplayer(200)).build(); dbUtils = DbUtils.create(context); } /** * */ private void initTheme() { int theme = (Integer) SP.get(context, "theme", 0); switch (theme) { case 0: setTheme(R.style.SwitchTheme0); break; case 1: setTheme(R.style.SwitchTheme1); break; case 2: setTheme(R.style.SwitchTheme2); break; case 3: setTheme(R.style.SwitchTheme3); break; } } /** * ? */ private void initLayout() { View v = getContentView(); ViewGroup rl_title = (ViewGroup) v.findViewById(R.id.rl_title);// TextView tv_title = (TextView) v.findViewById(R.id.tv_title);// ImageButton ib_back = (ImageButton) v.findViewById(R.id.ib_back);// ImageButton ib_right = (ImageButton) v.findViewById(R.id.ib_right);// ? View shadow = v.findViewById(R.id.shadow);// // if (rl_title != null && tv_title == null && ib_back == null && ib_right == null && shadow != null) { initTitleBar(rl_title, tv_title, ib_back, ib_right, shadow); // } setContentView(v); } /** * ?View * * @return */ protected abstract View getContentView(); /** * ?title * * @param rl_title * @param tv_title * @param ib_back * @param ib_right * @param shadow */ protected abstract void initTitleBar(ViewGroup rl_title, TextView tv_title, ImageButton ib_back, ImageButton ib_right, View shadow); /** * ?? */ @TargetApi(Build.VERSION_CODES.KITKAT) private void initStatusBar() { int theme = (Integer) SP.get(context, "theme", 0); int color = R.color.theme_0; switch (theme) { case 0: color = R.color.theme_0; break; case 1: color = R.color.theme_1; break; case 2: color = R.color.theme_2; break; case 3: color = R.color.theme_3; break; } SystemBarTintManager tintManager = new SystemBarTintManager(this); setStatusBarColor(tintManager, color); } protected void setStatusBarColor(SystemBarTintManager tintManager, int color) { tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintColor(getResources().getColor(color)); } /** * ?Fragment * * @param id ??id * @param fragment ??Fragment */ protected void changeFramgnt(int id, Fragment fragment) { getSupportFragmentManager().beginTransaction().replace(id, fragment).commit(); } /** * ?? */ public void updateUserInfos() { // ??? updateUserLocation(); // ?(????),???100?BmobConfig.LIMIT_CONTACTS?? // ??????, userManager.queryCurrentContactList(new FindListener<BmobChatUser>() { @Override public void onError(int arg0, String arg1) { L.i("?" + arg1); } @Override public void onSuccess(List<BmobChatUser> arg0) { // ?application CustomApplication.getInstance().setContactList(CollectionUtils.list2map(arg0)); } }); } /** * ?? */ public void updateUserLocation() { if (CustomApplication.lastPoint != null) { String saveLatitude = CustomApplication.lastPoint.getLatitude() + ""; String saveLongtitude = CustomApplication.lastPoint.getLongitude() + ""; String newLat = String.valueOf(CustomApplication.lastPoint.getLatitude()); String newLong = String.valueOf(CustomApplication.lastPoint.getLongitude()); if (!saveLatitude.equals(newLat) || !saveLongtitude.equals(newLong)) {// ????? final User user = (User) userManager.getCurrentUser(User.class); user.setLocation(CustomApplication.lastPoint); user.update(this, new UpdateListener() { @Override public void onSuccess() { CustomApplication.getInstance().setLocation(user.getLocation().getLongitude(), user.getLocation().getLatitude()); L.i("??"); } @Override public void onFailure(int code, String msg) { L.i("? :" + msg); } }); } else { L.i("???"); } } } /** * ?? hideSoftInputView */ public void hideSoftInputView() { InputMethodManager manager = ((InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE)); if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) { if (getCurrentFocus() != null) manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } /** * */ @Override public void onBackPressed() { A.finishSelf(context); } /** * * * @param v */ public void back(View v) { onBackPressed(); } }