refactor: remove unused resources, cleanup the code structure
This commit is contained in:
36
common/src/main/java/com/stardust/app/DialogUtils.java
Normal file
36
common/src/main/java/com/stardust/app/DialogUtils.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.stardust.app;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.ContextWrapper;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/8/4.
|
||||
*/
|
||||
|
||||
public class DialogUtils {
|
||||
|
||||
public static <T extends Dialog> T showDialog(T dialog) {
|
||||
Context context = dialog.getContext();
|
||||
if (!isActivityContext(context)) {
|
||||
Window window = dialog.getWindow();
|
||||
if (window != null)
|
||||
window.setType(WindowManager.LayoutParams.TYPE_PHONE);
|
||||
}
|
||||
dialog.show();
|
||||
return dialog;
|
||||
}
|
||||
|
||||
|
||||
public static boolean isActivityContext(Context context) {
|
||||
if (context instanceof Activity)
|
||||
return true;
|
||||
if (context instanceof ContextWrapper) {
|
||||
return isActivityContext(((ContextWrapper) context).getBaseContext());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
48
common/src/main/java/com/stardust/app/Fragment.java
Normal file
48
common/src/main/java/com/stardust/app/Fragment.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package com.stardust.app;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.stardust.util.ViewUtil;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/1/30.
|
||||
*/
|
||||
|
||||
public abstract class Fragment extends android.support.v4.app.Fragment {
|
||||
|
||||
private View mView;
|
||||
|
||||
@NonNull
|
||||
public View getView() {
|
||||
return mView;
|
||||
}
|
||||
|
||||
public <T extends View> T $(int id) {
|
||||
return ViewUtil.$(mView, id);
|
||||
}
|
||||
|
||||
public View findViewById(int id) {
|
||||
return mView.findViewById(id);
|
||||
}
|
||||
|
||||
public View getActivityContentView() {
|
||||
return getActivity().getWindow().getDecorView();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
mView = createView(inflater, container, savedInstanceState);
|
||||
return mView;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public abstract View createView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.stardust.app;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentPagerAdapter;
|
||||
import android.util.SparseArray;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/3/24.
|
||||
*/
|
||||
|
||||
public class FragmentPagerAdapterBuilder {
|
||||
|
||||
public interface OnFragmentInstantiateListener {
|
||||
void OnInstantiate(int pos, Fragment fragment);
|
||||
}
|
||||
|
||||
private List<Fragment> mFragments = new ArrayList<>();
|
||||
private List<String> mTitles = new ArrayList<>();
|
||||
private FragmentActivity mActivity;
|
||||
|
||||
public FragmentPagerAdapterBuilder(FragmentActivity activity) {
|
||||
mActivity = activity;
|
||||
}
|
||||
|
||||
public FragmentPagerAdapterBuilder add(Fragment fragment, String title) {
|
||||
mFragments.add(fragment);
|
||||
mTitles.add(title);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FragmentPagerAdapterBuilder add(Fragment fragment, int titleResId) {
|
||||
return add(fragment, mActivity.getString(titleResId));
|
||||
}
|
||||
|
||||
public StoredFragmentPagerAdapter build() {
|
||||
return new StoredFragmentPagerAdapter(mActivity.getSupportFragmentManager()) {
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
return mFragments.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return mFragments.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getPageTitle(int position) {
|
||||
return mTitles.get(position);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public abstract static class StoredFragmentPagerAdapter extends FragmentPagerAdapter {
|
||||
|
||||
private SparseArray<Fragment> mStoredFragments = new SparseArray<>();
|
||||
private OnFragmentInstantiateListener mOnFragmentInstantiateListener;
|
||||
|
||||
public StoredFragmentPagerAdapter(FragmentManager fm) {
|
||||
super(fm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object instantiateItem(ViewGroup container, int position) {
|
||||
Fragment fragment = (Fragment) super.instantiateItem(container, position);
|
||||
mStoredFragments.put(position, fragment);
|
||||
if(mOnFragmentInstantiateListener != null){
|
||||
mOnFragmentInstantiateListener.OnInstantiate(position, fragment);
|
||||
}
|
||||
return fragment;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void destroyItem(ViewGroup container, int position, Object object) {
|
||||
mStoredFragments.remove(position);
|
||||
super.destroyItem(container, position, object);
|
||||
}
|
||||
|
||||
public Fragment getStoredFragment(int position) {
|
||||
return mStoredFragments.get(position);
|
||||
}
|
||||
|
||||
public void setOnFragmentInstantiateListener(OnFragmentInstantiateListener onFragmentInstantiateListener) {
|
||||
mOnFragmentInstantiateListener = onFragmentInstantiateListener;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.stardust.app;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/4.
|
||||
*/
|
||||
|
||||
public class VolumeChangeObserver extends BroadcastReceiver {
|
||||
|
||||
public interface OnVolumeChangeListener {
|
||||
|
||||
void onVolumeChange();
|
||||
}
|
||||
|
||||
public static final String ACTION_VOLUME_CHANGE = "android.media.VOLUME_CHANGED_ACTION";
|
||||
|
||||
private long mLastChangeMillis;
|
||||
private List<OnVolumeChangeListener> mOnVolumeChangeListenerList = new ArrayList<>();
|
||||
|
||||
public void addOnVolumeChangeListener(OnVolumeChangeListener listener) {
|
||||
mOnVolumeChangeListenerList.add(listener);
|
||||
}
|
||||
|
||||
public void removeOnVolumeChangeListener(OnVolumeChangeListener listener) {
|
||||
mOnVolumeChangeListenerList.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equals(ACTION_VOLUME_CHANGE)) {
|
||||
if (System.currentTimeMillis() - mLastChangeMillis < 400) {
|
||||
return;
|
||||
}
|
||||
mLastChangeMillis = System.currentTimeMillis();
|
||||
for (OnVolumeChangeListener listener : mOnVolumeChangeListenerList) {
|
||||
listener.onVolumeChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user