opt
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package com.mogo.module.extensions;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.mogo.commons.mvp.MvpFragment;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020-01-05
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class ExtensionsFragment extends MvpFragment< ExtensionsView, ExtensionsPresenter > implements ExtensionsView {
|
||||
|
||||
private View mVoiceIcon;
|
||||
private View mVoiceMsg;
|
||||
|
||||
private TextView mTime;
|
||||
private TextView mDate;
|
||||
|
||||
private View mWeatherContainer;
|
||||
private ImageView mWeatherIcon;
|
||||
private TextView mWeatherTemp;
|
||||
private TextView mWeatherDesc;
|
||||
|
||||
private View mMsgContainer;
|
||||
private TextView mMsgCounter;
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
return R.layout.module_ext_layout_extensions;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initViews() {
|
||||
mVoiceIcon = findViewById( R.id.module_ext_id_voice );
|
||||
mVoiceMsg = findViewById( R.id.module_ext_id_voice_msg );
|
||||
|
||||
mVoiceIcon.setOnClickListener( new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick( View v ) {
|
||||
mVoiceMsg.performClick();
|
||||
}
|
||||
} );
|
||||
mVoiceMsg.setOnClickListener( new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick( View v ) {
|
||||
|
||||
}
|
||||
} );
|
||||
|
||||
mTime = findViewById( R.id.module_ext_id_time );
|
||||
mDate = findViewById( R.id.module_ext_id_date );
|
||||
|
||||
mWeatherContainer = findViewById( R.id.module_ext_id_weather_container );
|
||||
mWeatherIcon = findViewById( R.id.module_ext_id_weather_icon );
|
||||
mWeatherTemp = findViewById( R.id.module_ext_id_weather_temp );
|
||||
mWeatherDesc = findViewById( R.id.module_ext_id_weather_desc );
|
||||
|
||||
mMsgContainer = findViewById( R.id.module_ext_id_msg );
|
||||
mMsgCounter = findViewById( R.id.module_ext_id_msg_counter );
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected ExtensionsPresenter createPresenter() {
|
||||
return new ExtensionsPresenter( this );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated( @Nullable Bundle savedInstanceState ) {
|
||||
super.onActivityCreated( savedInstanceState );
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderTime( String date, String time ) {
|
||||
mDate.setText( date );
|
||||
mTime.setText( time );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderWeatherInfo( String temp, String desc, int iconId ) {
|
||||
if ( iconId != 0 ) {
|
||||
mWeatherIcon.setImageResource( iconId );
|
||||
mWeatherIcon.setVisibility( View.VISIBLE );
|
||||
} else {
|
||||
mWeatherIcon.setVisibility( View.GONE );
|
||||
}
|
||||
mWeatherTemp.setText( temp );
|
||||
mWeatherDesc.setText( desc );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.mogo.module.extensions;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020-01-05
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class ExtensionsModuleConst {
|
||||
|
||||
public static final String PATH_EXTENSION = "/extension/ui";
|
||||
|
||||
|
||||
public static final String TYPE = "extension";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.mogo.module.extensions;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.alibaba.android.arouter.facade.annotation.Route;
|
||||
import com.mogo.map.listener.IMogoMapListener;
|
||||
import com.mogo.map.location.IMogoLocationListener;
|
||||
import com.mogo.map.navi.IMogoNaviListener;
|
||||
import com.mogo.service.module.IMogoModuleLifecycle;
|
||||
import com.mogo.service.module.IMogoModuleProvider;
|
||||
import com.mogo.service.module.ModuleType;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020-01-05
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
@Route( path = ExtensionsModuleConst.PATH_EXTENSION )
|
||||
public class ExtensionsModuleProvider implements IMogoModuleProvider {
|
||||
|
||||
@Override
|
||||
public Fragment createFragment( Context context, Bundle data ) {
|
||||
ExtensionsFragment fragment = new ExtensionsFragment();
|
||||
fragment.setArguments( data );
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View createView( Context context ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String getModuleName() {
|
||||
return ExtensionsModuleConst.TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMogoModuleLifecycle getCardLifecycle() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMogoMapListener getMapListener() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getType() {
|
||||
return ModuleType.TYPE_EXTENSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMogoNaviListener getNaviListener() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMogoLocationListener getLocationListener() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init( Context context ) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.mogo.module.extensions;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
|
||||
import com.mogo.commons.mvp.Presenter;
|
||||
import com.mogo.module.extensions.weather.Phenomena;
|
||||
import com.mogo.module.extensions.weather.WeatherCallback;
|
||||
import com.mogo.module.extensions.weather.WeatherInfo;
|
||||
import com.mogo.module.extensions.weather.WeatherModel;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020-01-05
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class ExtensionsPresenter extends Presenter< ExtensionsView > implements WeatherCallback {
|
||||
|
||||
private static final String TAG = "ExtensionsPresenter";
|
||||
|
||||
private String[] mWeeks;
|
||||
|
||||
private WeatherModel mWeatherModel;
|
||||
|
||||
/**
|
||||
* 接收时间变化的广播
|
||||
*/
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive( Context context, Intent intent ) {
|
||||
try {
|
||||
refreshTimeAndDate();
|
||||
} catch ( Exception e ) {
|
||||
Logger.e( TAG, "error. ", e );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
public ExtensionsPresenter( ExtensionsView view ) {
|
||||
super( view );
|
||||
mWeeks = getContext().getResources().getStringArray( R.array.module_ext_str_arr_week );
|
||||
mWeatherModel = new WeatherModel( getContext() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate( @NonNull LifecycleOwner owner ) {
|
||||
super.onCreate( owner );
|
||||
registerTimerReceiver();
|
||||
mWeatherModel.init( this );
|
||||
mWeatherModel.queryWeatherInformation();
|
||||
refreshTimeAndDate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册时间变化监听
|
||||
*/
|
||||
private void registerTimerReceiver() {
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction( Intent.ACTION_TIME_TICK );
|
||||
filter.addAction( Intent.ACTION_TIME_CHANGED );
|
||||
filter.addAction( Intent.ACTION_TIMEZONE_CHANGED );
|
||||
filter.addAction( Intent.ACTION_CONFIGURATION_CHANGED );
|
||||
getContext().registerReceiver( mReceiver, filter );
|
||||
}
|
||||
|
||||
private void refreshTimeAndDate() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
int hour = calendar.get( Calendar.HOUR_OF_DAY );
|
||||
int minute = calendar.get( Calendar.MINUTE );
|
||||
|
||||
int month = calendar.get( Calendar.MONTH );
|
||||
int day = calendar.get( Calendar.DAY_OF_MONTH );
|
||||
int week = calendar.get( Calendar.DAY_OF_WEEK );
|
||||
|
||||
String timeStr = getContext().getResources().getString( R.string.module_ext_str_time_format, hour, minute );
|
||||
String dateStr = getContext().getResources().getString( R.string.module_ext_str_date_format, month + 1, day, mWeeks[week - 1] );
|
||||
mView.renderTime( dateStr, timeStr );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWeatherLoaded( WeatherInfo weatherInfo ) {
|
||||
if ( weatherInfo == null ) {
|
||||
return;
|
||||
}
|
||||
String temp = getContext().getResources().getString( R.string.module_ext_str_weather_temp_format, weatherInfo.getTemperature() );
|
||||
Phenomena phenomena = Phenomena.getById( weatherInfo.getPhenomena() );
|
||||
String desc = phenomena == null ? "" : phenomena.nameCn;
|
||||
int resId = 0;
|
||||
mView.renderWeatherInfo( temp, desc, resId );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy( @NonNull LifecycleOwner owner ) {
|
||||
super.onDestroy( owner );
|
||||
if ( mWeatherModel != null ) {
|
||||
mWeatherModel.destroy();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.mogo.module.extensions;
|
||||
|
||||
import com.mogo.commons.mvp.IView;
|
||||
import com.mogo.module.extensions.weather.WeatherInfo;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020-01-05
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public interface ExtensionsView extends IView {
|
||||
|
||||
/**
|
||||
* 刷新日期、时间
|
||||
*
|
||||
* @param date 日期
|
||||
* @param time 时间
|
||||
*/
|
||||
void renderTime( String date, String time );
|
||||
|
||||
/**
|
||||
* 天气信息
|
||||
*
|
||||
* @param desc 天气描述:晴转多云
|
||||
* @param temp 温度
|
||||
* @param iconId 图标
|
||||
*/
|
||||
void renderWeatherInfo( String temp, String desc, int iconId );
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.mogo.module.extensions.weather;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Lzq
|
||||
*/
|
||||
public enum Phenomena {
|
||||
Sunny( "00", "晴", "Sunny" ),
|
||||
Cloudy( "01", "多云", "Cloudy" ),
|
||||
Overcast( "02", "阴", "Overcast" ),
|
||||
|
||||
Shower( "03", "阵雨", "Shower" ),
|
||||
Thundershower( "04", "雷阵雨", "Thundershower" ),
|
||||
ThundershowerWithHail( "05", "雷阵雨伴有冰雹", "Thundershower with hail" ),
|
||||
Sleet( "06", "雨夹雪", "Sleet" ),
|
||||
LightRain( "07", "小雨", "Light rain" ),
|
||||
ModerateRain( "08", "中雨", "Moderate rain" ),
|
||||
HeavyRain( "09", "大雨", "Heavy rain" ),
|
||||
Storm( "10", "暴雨", "Storm" ),
|
||||
HeavyStorm( "11", "大暴雨", "Heavy storm" ),
|
||||
SevereStorm( "12", "特大暴雨", "Severe storm" ),
|
||||
|
||||
SnowFlurry( "13", "阵雪", "Snow flurry" ),
|
||||
LightSnow( "14", "小雪", "Light snow" ),
|
||||
ModerateSnow( "15", "中雪", "Moderate snow" ),
|
||||
HeavySnow( "16", "大雪", "Heavy snow" ),
|
||||
Snowstorm( "17", "暴雪", "Snowstorm" ),
|
||||
|
||||
Foggy( "18", "雾", "Foggy" ),
|
||||
IceRain( "19", "冻雨", "Ice rain" ),
|
||||
Duststorm( "20", "沙尘暴", "Duststorm" ),
|
||||
|
||||
LightToModerateRain( "21", "小到中雨", "Light to moderate rain" ),
|
||||
ModerateToHeavyRain( "22", "中到大雨", "Moderate to heavy rain" ),
|
||||
HeavyRainToStorm( "23", "大到大雨", "Heavy rain to storm" ),
|
||||
StormToHeavyStorm( "24", "暴雨到大暴雨", "Storm to heavy storm" ),
|
||||
HeavyToSevereStorm( "25", "大暴雨到特大暴雨", "Heavy to severe storm" ),
|
||||
|
||||
LightToModerateSnow( "26", "小到中雪", "Light to moderate snow" ),
|
||||
ModerateToHeavySnow( "27", "中到大雪", "Moderate to heavy snow" ),
|
||||
HeavySnowToSnowStorm( "28", "大到暴雪", "Heavy snow to snowstorm" ),
|
||||
|
||||
Dust( "29", "浮尘", "Dust" ),
|
||||
Sand( "30", "扬沙", "Sand" ),
|
||||
SandStorm( "31", "强沙尘暴", "Sandstorm" ),
|
||||
|
||||
Densefog( "32", "浓雾", "Dense fog" ),
|
||||
StrongFog( "49", "强浓雾", "Strong fog" ),
|
||||
DenseFog( "57", "大雾", "Dense fog" ),
|
||||
ExtraHeavyFog( "58", "特强浓雾", "Extra heavy fog" ),
|
||||
|
||||
Haze( "53", "霾", "Haze" ),
|
||||
ModerateHaze( "54", "中度霾", "Moderate haze" ),
|
||||
Severehaze( "55", "重度霾", "Severe haze" ),
|
||||
SevereHaze( "56", "严重霾", "Severe haze" ),
|
||||
|
||||
Unknown( "99", "无", "Unknown" ),
|
||||
|
||||
Rain( "301", "雨", "rain" ),
|
||||
Snow( "302", "雪", "snow" );
|
||||
|
||||
public final String id;
|
||||
public final String nameCn;
|
||||
public final String nameEn;
|
||||
|
||||
Phenomena( String id, String nameCn, String nameEn ) {
|
||||
this.id = id;
|
||||
this.nameCn = nameCn;
|
||||
this.nameEn = nameEn;
|
||||
}
|
||||
|
||||
static Map< String, Phenomena > mPhenomenas;
|
||||
|
||||
static {
|
||||
if ( mPhenomenas == null ) {
|
||||
synchronized ( Phenomena.class ) {
|
||||
if ( mPhenomenas == null ) {
|
||||
mPhenomenas = new HashMap<>();
|
||||
for ( Phenomena weather : Phenomena.values() ) {
|
||||
mPhenomenas.put( weather.id, weather );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized Phenomena getById( String id ) {
|
||||
if ( TextUtils.isEmpty( id ) ) {
|
||||
return null;
|
||||
}
|
||||
return mPhenomenas.get( id );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.mogo.module.extensions.weather;
|
||||
|
||||
public interface WeatherCallback {
|
||||
void onWeatherLoaded( WeatherInfo weatherInfo );
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.mogo.module.extensions.weather;
|
||||
|
||||
/**
|
||||
* 天气
|
||||
*/
|
||||
public class WeatherConstants {
|
||||
|
||||
public final static String WEATHER_URI = "content://com.zhidao.weather/weatherinfo";
|
||||
|
||||
/**
|
||||
* 天气
|
||||
*/
|
||||
public static final String TEMPERATURE = "observetemperature";
|
||||
/**
|
||||
* 气象
|
||||
*/
|
||||
public static final String PHENOMENA = "observephenomena";
|
||||
/**
|
||||
* 风向
|
||||
*/
|
||||
public static final String WIND_DIRECTION = "observewinddirection";
|
||||
/**
|
||||
* 风力
|
||||
*/
|
||||
public static final String WIND_FORCE = "observewindforce";
|
||||
|
||||
/**
|
||||
* 天气消息加载完毕
|
||||
*/
|
||||
public static final int MSG_WEATHER_LOADED = 0x1000;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
package com.mogo.module.extensions.weather;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 天气
|
||||
*/
|
||||
public class WeatherInfo implements Parcelable {
|
||||
/**
|
||||
* 温度
|
||||
*/
|
||||
private String temperature;
|
||||
|
||||
/**
|
||||
* 描述信息
|
||||
*/
|
||||
private String phenomena;
|
||||
|
||||
/**
|
||||
* 风向
|
||||
*/
|
||||
private String windDirection;
|
||||
|
||||
/**
|
||||
* 风力
|
||||
*/
|
||||
private String windForce;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WeatherInfo{" +
|
||||
"temperature='" + temperature + '\'' +
|
||||
", phenomena='" + phenomena + '\'' +
|
||||
", windDirection='" + windDirection + '\'' +
|
||||
", windForce='" + windForce + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals( Object o ) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( !( o instanceof WeatherInfo ) ) {
|
||||
return false;
|
||||
}
|
||||
WeatherInfo that = ( WeatherInfo ) o;
|
||||
return Objects.equals( temperature, that.temperature ) &&
|
||||
Objects.equals( phenomena, that.phenomena ) &&
|
||||
Objects.equals( windDirection, that.windDirection ) &&
|
||||
Objects.equals( windForce, that.windForce );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( temperature, phenomena, windDirection, windForce );
|
||||
}
|
||||
|
||||
public WeatherInfo() {
|
||||
}
|
||||
|
||||
public WeatherInfo( String temperature, String phenomena, String windDirection, String windForce ) {
|
||||
this.temperature = temperature;
|
||||
this.phenomena = phenomena;
|
||||
this.windDirection = windDirection;
|
||||
this.windForce = windForce;
|
||||
}
|
||||
|
||||
public String getTemperature() {
|
||||
return temperature;
|
||||
}
|
||||
|
||||
public void setTemperature( String temperature ) {
|
||||
this.temperature = temperature;
|
||||
}
|
||||
|
||||
public String getPhenomena() {
|
||||
return phenomena;
|
||||
}
|
||||
|
||||
public void setPhenomena( String phenomena ) {
|
||||
this.phenomena = phenomena;
|
||||
}
|
||||
|
||||
public String getWindDirection() {
|
||||
return windDirection;
|
||||
}
|
||||
|
||||
public void setWindDirection( String windDirection ) {
|
||||
this.windDirection = windDirection;
|
||||
}
|
||||
|
||||
public String getWindForce() {
|
||||
return windForce;
|
||||
}
|
||||
|
||||
public void setWindForce( String windForce ) {
|
||||
this.windForce = windForce;
|
||||
}
|
||||
|
||||
public boolean isLegal() {
|
||||
return !TextUtils.isEmpty( phenomena )
|
||||
&& !TextUtils.isEmpty( temperature );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel( Parcel dest, int flags ) {
|
||||
dest.writeString( this.temperature );
|
||||
dest.writeString( this.phenomena );
|
||||
dest.writeString( this.windDirection );
|
||||
dest.writeString( this.windForce );
|
||||
}
|
||||
|
||||
protected WeatherInfo( Parcel in ) {
|
||||
this.temperature = in.readString();
|
||||
this.phenomena = in.readString();
|
||||
this.windDirection = in.readString();
|
||||
this.windForce = in.readString();
|
||||
}
|
||||
|
||||
public static final Creator< WeatherInfo > CREATOR = new Creator< WeatherInfo >() {
|
||||
@Override
|
||||
public WeatherInfo createFromParcel( Parcel source ) {
|
||||
return new WeatherInfo( source );
|
||||
}
|
||||
|
||||
@Override
|
||||
public WeatherInfo[] newArray( int size ) {
|
||||
return new WeatherInfo[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.mogo.module.extensions.weather;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.mogo.utils.ThreadPoolService;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020-01-05
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class WeatherModel {
|
||||
|
||||
private static final String TAG = "WeatherModel";
|
||||
|
||||
private Context mContext;
|
||||
|
||||
private Uri mWeatherUri;
|
||||
private Handler mHandler;
|
||||
private ContentResolver mContentResolver;
|
||||
private ContentObserver mContentObserver;
|
||||
private WeatherCallback mCallback;
|
||||
|
||||
public WeatherModel( Context context ) {
|
||||
this.mContext = context;
|
||||
}
|
||||
|
||||
public void init( WeatherCallback callback ) {
|
||||
mCallback = callback;
|
||||
mWeatherUri = Uri.parse( WeatherConstants.WEATHER_URI );
|
||||
mContentResolver = mContext.getContentResolver();
|
||||
mHandler = new Handler( Looper.getMainLooper() ) {
|
||||
@Override
|
||||
public void handleMessage( @NonNull Message msg ) {
|
||||
if ( msg.what == WeatherConstants.MSG_WEATHER_LOADED ) {
|
||||
if ( mCallback != null ) {
|
||||
mCallback.onWeatherLoaded( ( ( WeatherInfo ) msg.obj ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
mContentObserver = new ContentObserver( mHandler ) {
|
||||
@Override
|
||||
public void onChange( boolean selfChange, Uri uri ) {
|
||||
super.onChange( selfChange, uri );
|
||||
try {
|
||||
queryWeatherInformation();
|
||||
} catch ( Exception e ) {
|
||||
Logger.e( TAG, "error. ", e );
|
||||
}
|
||||
}
|
||||
};
|
||||
mContentResolver.registerContentObserver( mWeatherUri, false, mContentObserver );
|
||||
}
|
||||
|
||||
public void queryWeatherInformation() {
|
||||
|
||||
if ( mCallback == null ) {
|
||||
Logger.e( TAG, "WeatherModel#init should invoked " );
|
||||
return;
|
||||
}
|
||||
startNewThreadToQuery();
|
||||
}
|
||||
|
||||
|
||||
private void startNewThreadToQuery() {
|
||||
ThreadPoolService.execute( new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if ( mContentResolver == null ) {
|
||||
return;
|
||||
}
|
||||
Cursor cursor = mContentResolver.query( mWeatherUri, null, null, null, null, null );
|
||||
if ( cursor == null ) {
|
||||
return;
|
||||
}
|
||||
WeatherInfo weatherInfo = new WeatherInfo();
|
||||
if ( cursor.moveToFirst() ) {
|
||||
int index = cursor.getColumnIndex( WeatherConstants.TEMPERATURE );
|
||||
if ( index != -1 ) {
|
||||
weatherInfo.setTemperature( cursor.getString( index ) );
|
||||
}
|
||||
index = cursor.getColumnIndex( WeatherConstants.PHENOMENA );
|
||||
if ( index != -1 ) {
|
||||
weatherInfo.setPhenomena( cursor.getString( index ) );
|
||||
}
|
||||
index = cursor.getColumnIndex( WeatherConstants.WIND_DIRECTION );
|
||||
if ( index != -1 ) {
|
||||
weatherInfo.setWindDirection( cursor.getString( index ) );
|
||||
}
|
||||
index = cursor.getColumnIndex( WeatherConstants.WIND_FORCE );
|
||||
if ( index != -1 ) {
|
||||
weatherInfo.setWindForce( cursor.getString( index ) );
|
||||
}
|
||||
Message msg = Message.obtain();
|
||||
msg.obj = weatherInfo;
|
||||
msg.what = WeatherConstants.MSG_WEATHER_LOADED;
|
||||
mHandler.sendMessage( msg );
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
if ( mContentResolver != null && mContentObserver != null ) {
|
||||
mContentResolver.unregisterContentObserver( mContentObserver );
|
||||
}
|
||||
mContext = null;
|
||||
mWeatherUri = null;
|
||||
mHandler = null;
|
||||
mContentResolver = null;
|
||||
mContentObserver = null;
|
||||
mCallback = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user