@ -103,6 +103,7 @@ import java.util.HashSet;
import java.util.List ;
import java.util.Locale ;
import java.util.Map ;
import java.util.Vector ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
@ -135,6 +136,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
}
private static final Map < Integer , Integer > sBgSelectorBtnsMap = new HashMap < Integer , Integer > ( ) ;
static {
sBgSelectorBtnsMap . put ( R . id . iv_bg_yellow , ResourceParser . YELLOW ) ;
sBgSelectorBtnsMap . put ( R . id . iv_bg_red , ResourceParser . RED ) ;
@ -144,6 +146,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
}
private static final Map < Integer , Integer > sBgSelectorSelectionMap = new HashMap < Integer , Integer > ( ) ;
static {
sBgSelectorSelectionMap . put ( ResourceParser . YELLOW , R . id . iv_bg_yellow_select ) ;
sBgSelectorSelectionMap . put ( ResourceParser . RED , R . id . iv_bg_red_select ) ;
@ -153,6 +156,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
}
private static final Map < Integer , Integer > sFontSizeBtnsMap = new HashMap < Integer , Integer > ( ) ;
static {
sFontSizeBtnsMap . put ( R . id . ll_font_large , ResourceParser . TEXT_LARGE ) ;
sFontSizeBtnsMap . put ( R . id . ll_font_small , ResourceParser . TEXT_SMALL ) ;
@ -161,6 +165,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
}
private static final Map < Integer , Integer > sFontSelectorSelectionMap = new HashMap < Integer , Integer > ( ) ;
static {
sFontSelectorSelectionMap . put ( ResourceParser . TEXT_LARGE , R . id . iv_large_select ) ;
sFontSelectorSelectionMap . put ( ResourceParser . TEXT_SMALL , R . id . iv_small_select ) ;
@ -188,7 +193,6 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
private int mFontSizeId ;
private static final String PREFERENCE_FONT_SIZE = "pref_font_size" ;
private static final int SHORTCUT_ICON_TITLE_MAX_LEN = 10 ;
@ -200,7 +204,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
public static final int CHOOSE_PHOTO = 2 ;
private final int PHOTO_SUCCESS = 3 ;
private final int CAMERA_SUCCESS = 4 ;
// TextToSpeech tts;
// TextToSpeech tts;
TextView num_word ;
TextView location ;
public static final int LOCATION_CODE = 301 ;
@ -210,6 +214,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
private AlertDialog alertDialog2 ;
private CharSequence restore_translate = null ;
private boolean mIsRvoke = false ;
private LinearLayout mEditTextList ;
@ -244,9 +249,6 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
}
/ * *
* Current activity may be killed when the memory is low . Once it is killed , for another time
* user load this activity , we should restore the former state
@ -300,7 +302,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
getWindow ( ) . setSoftInputMode (
WindowManager . LayoutParams . SOFT_INPUT_STATE_HIDDEN
| WindowManager . LayoutParams . SOFT_INPUT_ADJUST_RESIZE ) ;
} else if ( TextUtils . equals ( Intent . ACTION_INSERT_OR_EDIT , intent . getAction ( ) ) ) {
} else if ( TextUtils . equals ( Intent . ACTION_INSERT_OR_EDIT , intent . getAction ( ) ) ) {
// New note
long folderId = intent . getLongExtra ( Notes . INTENT_EXTRA_FOLDER_ID , 0 ) ;
int widgetId = intent . getIntExtra ( Notes . INTENT_EXTRA_WIDGET_ID ,
@ -400,11 +402,11 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
}
}
private void showTopHeader ( ) {
private void showTopHeader ( ) {
mNoteHeaderHolder . tvToptext . setText ( R . string . menu_set_top ) ;
if ( mWorkingNote . getTopId ( ) = = 1 ) {
if ( mWorkingNote . getTopId ( ) = = 1 ) {
mNoteHeaderHolder . tvToptext . setVisibility ( View . VISIBLE ) ;
} else {
} else {
mNoteHeaderHolder . tvToptext . setVisibility ( View . GONE ) ;
}
}
@ -447,7 +449,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
}
private boolean inRangeOfView ( View view , MotionEvent ev ) {
int [ ] location = new int [ 2 ] ;
int [ ] location = new int [ 2 ] ;
view . getLocationOnScreen ( location ) ;
int x = location [ 0 ] ;
int y = location [ 1 ] ;
@ -468,8 +470,28 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
mNoteHeaderHolder . tvAlertDate = ( TextView ) findViewById ( R . id . tv_alert_date ) ;
mNoteHeaderHolder . tvToptext = ( TextView ) findViewById ( R . id . tv_set_top ) ;
mNoteHeaderHolder . ibSetBgColor = ( ImageView ) findViewById ( R . id . btn_set_bg_color ) ;
mNoteHeaderHolder . ibSetBgColor . setOnClickListener ( this ) ;
mNoteEditor = ( EditText ) findViewById ( R . id . note_edit_view ) ;
mNoteEditor . addTextChangedListener ( new TextWatcher ( ) {
@Override
public void beforeTextChanged ( CharSequence s , int start , int count , int after ) {
}
@Override
public void onTextChanged ( CharSequence s , int start , int before , int count ) {
}
@Override
public void afterTextChanged ( Editable s ) { //文本更改后
if ( ! mIsRvoke ) {
saveMyChanged ( ) ;
} else {
mIsRvoke = false ;
}
}
} ) ;
mNoteEditorPanel = findViewById ( R . id . sv_note_edit ) ;
mNoteBgColorSelector = findViewById ( R . id . note_bg_color_selector ) ;
for ( int id : sBgSelectorBtnsMap . keySet ( ) ) {
@ -481,7 +503,8 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
for ( int id : sFontSizeBtnsMap . keySet ( ) ) {
View view = findViewById ( id ) ;
view . setOnClickListener ( this ) ;
} ;
}
;
mSharedPrefs = PreferenceManager . getDefaultSharedPreferences ( this ) ;
mFontSizeId = mSharedPrefs . getInt ( PREFERENCE_FONT_SIZE , ResourceParser . BG_DEFAULT_FONT_SIZE ) ;
/ * *
@ -489,7 +512,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
* * The id may larger than the length of resources , in this case ,
* * return the { @link ResourceParser # BG_DEFAULT_FONT_SIZE }
* /
if ( mFontSizeId > = TextAppearanceResources . getResourcesSize ( ) ) {
if ( mFontSizeId > = TextAppearanceResources . getResourcesSize ( ) ) {
mFontSizeId = ResourceParser . BG_DEFAULT_FONT_SIZE ;
}
mEditTextList = ( LinearLayout ) findViewById ( R . id . note_edit_list ) ;
@ -498,12 +521,13 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
speech_sum ( ) ;
local_sel ( ) ;
translate ( ) ;
}
@Override
protected void onPause ( ) {
super . onPause ( ) ;
if ( saveNote ( ) ) {
if ( saveNote ( ) ) {
Log . d ( TAG , "Note data was saved with length:" + mWorkingNote . getContent ( ) . length ( ) ) ;
}
clearSettingState ( ) ;
@ -511,7 +535,6 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
}
private void updateWidget ( ) {
Intent intent = new Intent ( AppWidgetManager . ACTION_APPWIDGET_UPDATE ) ;
if ( mWorkingNote . getWidgetType ( ) = = Notes . TYPE_WIDGET_2X ) {
@ -523,7 +546,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
return ;
}
intent . putExtra ( AppWidgetManager . EXTRA_APPWIDGET_IDS , new int [ ] {
intent . putExtra ( AppWidgetManager . EXTRA_APPWIDGET_IDS , new int [ ] {
mWorkingNote . getWidgetId ( )
} ) ;
@ -560,7 +583,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
@Override
public void onBackPressed ( ) {
if ( clearSettingState ( ) ) {
if ( clearSettingState ( ) ) {
return ;
}
@ -598,9 +621,9 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
} else {
getMenuInflater ( ) . inflate ( R . menu . note_edit , menu ) ;
}
if ( mWorkingNote . getTopId ( ) = = 1 ) {
if ( mWorkingNote . getTopId ( ) = = 1 ) {
menu . findItem ( R . id . menu_set_top ) . setVisible ( false ) ;
} else if ( mWorkingNote . getTopId ( ) = = 0 ) {
} else if ( mWorkingNote . getTopId ( ) = = 0 ) {
menu . findItem ( R . id . menu_cancel_top ) . setVisible ( false ) ;
}
if ( mWorkingNote . getCheckListMode ( ) = = TextNote . MODE_CHECK_LIST ) {
@ -622,11 +645,14 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
case R . id . menu_new_note :
createNewNote ( ) ;
break ;
case R . id . menu_revoke :
doRevoke ( ) ;
break ;
case R . id . menu_set_top :
mWorkingNote . setTop ( ( mWorkingNote . getTopId ( ) ) = = 1 ? "0" : "1" ) ;
mWorkingNote . setTop ( ( mWorkingNote . getTopId ( ) ) = = 1 ? "0" : "1" ) ;
break ;
case R . id . menu_cancel_top :
mWorkingNote . setTop ( ( mWorkingNote . getTopId ( ) ) = = 0 ? "1" : "0" ) ;
mWorkingNote . setTop ( ( mWorkingNote . getTopId ( ) ) = = 0 ? "1" : "0" ) ;
break ;
case R . id . menu_delete :
AlertDialog . Builder builder = new AlertDialog . Builder ( this ) ;
@ -667,6 +693,21 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
case R . id . menu_font_select :
showSingleAlertDiglog ( ) ;
break ;
case R . id . menu_count_word : //数字统计
AlertDialog . Builder builder1 = new AlertDialog . Builder ( this ) ; //对话框对象
builder1 . setIcon ( android . R . drawable . ic_dialog_alert ) ; //对话框图标
TextView content = ( TextView ) findViewById ( R . id . note_edit_view ) ;
int c = content . length ( ) ;
builder1 . setMessage ( "带符号字数总和统计为:" + c ) ;
builder1 . setPositiveButton ( android . R . string . ok ,
new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int which ) {
finish ( ) ;
}
} ) ; //确定按钮
builder1 . setNegativeButton ( android . R . string . cancel , null ) ; //取消按钮
builder1 . show ( ) ; //对话框启动
break ;
default :
break ;
}
@ -677,7 +718,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
DateTimePickerDialog d = new DateTimePickerDialog ( this , System . currentTimeMillis ( ) ) ;
d . setOnDateTimeSetListener ( new OnDateTimeSetListener ( ) {
public void OnDateTimeSet ( AlertDialog dialog , long date ) {
mWorkingNote . setAlertDate ( date , true ) ;
mWorkingNote . setAlertDate ( date , true ) ;
}
} ) ;
d . show ( ) ;
@ -732,7 +773,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
return NotesPreferenceActivity . getSyncAccountName ( this ) . trim ( ) . length ( ) > 0 ;
}
public void onTopChanged ( String Top ) {
public void onTopChanged ( String Top ) {
/ * *
* User could set clock to an unsaved note , so before setting the
* alert clock , we should save the note first
@ -766,7 +807,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
PendingIntent pendingIntent = PendingIntent . getBroadcast ( this , 0 , intent , 0 ) ;
AlarmManager alarmManager = ( ( AlarmManager ) getSystemService ( ALARM_SERVICE ) ) ;
showAlertHeader ( ) ;
if ( ! set ) {
if ( ! set ) {
alarmManager . cancel ( pendingIntent ) ;
} else {
alarmManager . set ( AlarmManager . RTC_WAKEUP , date , pendingIntent ) ;
@ -799,7 +840,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
mEditTextList . removeViewAt ( index ) ;
NoteEditText edit = null ;
if ( index = = 0 ) {
if ( index = = 0 ) {
edit = ( NoteEditText ) mEditTextList . getChildAt ( 0 ) . findViewById (
R . id . et_edit_text ) ;
} else {
@ -816,7 +857,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
/ * *
* Should not happen , check for debug
* /
if ( index > mEditTextList . getChildCount ( ) ) {
if ( index > mEditTextList . getChildCount ( ) ) {
Log . e ( TAG , "Index out of mEditTextList boundrary, should not happen" ) ;
}
@ -836,7 +877,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
String [ ] items = text . split ( "\n" ) ;
int index = 0 ;
for ( String item : items ) {
if ( ! TextUtils . isEmpty ( item ) ) {
if ( ! TextUtils . isEmpty ( item ) ) {
mEditTextList . addView ( getListItem ( item , index ) ) ;
index + + ;
}
@ -901,7 +942,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
Log . e ( TAG , "Wrong index, should not happen" ) ;
return ;
}
if ( hasText ) {
if ( hasText ) {
mEditTextList . getChildAt ( index ) . findViewById ( R . id . cb_edit_item ) . setVisibility ( View . VISIBLE ) ;
} else {
mEditTextList . getChildAt ( index ) . findViewById ( R . id . cb_edit_item ) . setVisibility ( View . GONE ) ;
@ -1012,158 +1053,155 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
}
private String getLocation ( ) {
//1.获取位置管理器
String city = null ;
locationManager = ( LocationManager ) getSystemService ( Context . LOCATION_SERVICE ) ;
//2.获取位置提供器, GPS或是NetWork
List < String > providers = locationManager . getProviders ( true ) ;
private String getLocation ( ) {
//1.获取位置管理器
String city = null ;
locationManager = ( LocationManager ) getSystemService ( Context . LOCATION_SERVICE ) ;
//2.获取位置提供器, GPS或是NetWork
List < String > providers = locationManager . getProviders ( true ) ;
if ( providers . contains ( LocationManager . GPS_PROVIDER ) ) {
//如果是GPS
locationProvider = LocationManager . GPS_PROVIDER ;
Log . v ( "TAG" , "定位方式GPS" ) ;
} else if ( providers . contains ( LocationManager . NETWORK_PROVIDER ) ) {
//如果是Network
locationProvider = LocationManager . NETWORK_PROVIDER ;
Log . v ( "TAG" , "定位方式Network" ) ;
} else {
Toast . makeText ( this , "没有可用的位置提供器" , Toast . LENGTH_SHORT ) . show ( ) ;
return null ;
}
if ( providers . contains ( LocationManager . GPS_PROVIDER ) ) {
//如果是GPS
locationProvider = LocationManager . GPS_PROVIDER ;
Log . v ( "TAG" , "定位方式GPS" ) ;
} else if ( providers . contains ( LocationManager . NETWORK_PROVIDER ) ) {
//如果是Network
locationProvider = LocationManager . NETWORK_PROVIDER ;
Log . v ( "TAG" , "定位方式Network" ) ;
} else {
Toast . makeText ( this , "没有可用的位置提供器" , Toast . LENGTH_SHORT ) . show ( ) ;
return null ;
}
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . M ) {
//获取权限(如果没有开启权限,会弹出对话框,询问是否开启权限)
if ( ContextCompat . checkSelfPermission ( this , Manifest . permission . ACCESS_FINE_LOCATION )
! = PackageManager . PERMISSION_GRANTED | |
ActivityCompat . checkSelfPermission ( this , Manifest . permission . ACCESS_COARSE_LOCATION )
! = PackageManager . PERMISSION_GRANTED ) {
//请求权限
ActivityCompat . requestPermissions ( this , new String [ ] { Manifest . permission . ACCESS_FINE_LOCATION ,
Manifest . permission . ACCESS_COARSE_LOCATION } , LOCATION_CODE ) ;
} else {
//3.获取上次的位置, 一般第一次运行, 此值为null
Location location = locationManager . getLastKnownLocation ( locationProvider ) ;
if ( location ! = null ) {
Toast . makeText ( this , location . getLongitude ( ) + " " +
location . getLatitude ( ) + "" , Toast . LENGTH_SHORT ) . show ( ) ;
Log . v ( "TAG" , "获取上次的位置-经纬度:" + location . getLongitude ( ) + " " + location . getLatitude ( ) ) ;
city = getAddress ( location ) ;
} else {
//监视地理位置变化, 第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace
locationManager . requestLocationUpdates ( locationProvider , 3000 , 1 , locationListener ) ;
}
}
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . M ) {
//获取权限(如果没有开启权限,会弹出对话框,询问是否开启权限)
if ( ContextCompat . checkSelfPermission ( this , Manifest . permission . ACCESS_FINE_LOCATION )
! = PackageManager . PERMISSION_GRANTED | |
ActivityCompat . checkSelfPermission ( this , Manifest . permission . ACCESS_COARSE_LOCATION )
! = PackageManager . PERMISSION_GRANTED ) {
//请求权限
ActivityCompat . requestPermissions ( this , new String [ ] { Manifest . permission . ACCESS_FINE_LOCATION ,
Manifest . permission . ACCESS_COARSE_LOCATION } , LOCATION_CODE ) ;
} else {
//3.获取上次的位置, 一般第一次运行, 此值为null
Location location = locationManager . getLastKnownLocation ( locationProvider ) ;
if ( location ! = null ) {
if ( location ! = null ) {
Toast . makeText ( this , location . getLongitude ( ) + " " +
location . getLatitude ( ) + "" , Toast . LENGTH_SHORT ) . show ( ) ;
Log . v ( "TAG" , "获取上次的位置-经纬度:" + location . getLongitude ( ) + " " + location . getLatitude ( ) ) ;
city = getAddress ( location ) ;
Log . v ( "TAG" , "获取上次的位置-经纬度:" + location . getLongitude ( ) + " " + location . getLatitude ( ) ) ;
city = getAddress ( location ) ;
} else {
} else {
//监视地理位置变化, 第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace
locationManager . requestLocationUpdates ( locationProvider , 3000 , 1 , locationListener ) ;
locationManager . requestLocationUpdates ( locationProvider , 3000 , 1 , locationListener ) ;
}
}
return city ;
}
} else {
Location location = locationManager . getLastKnownLocation ( locationProvider ) ;
if ( location ! = null ) {
Toast . makeText ( this , location . getLongitude ( ) + " " +
location . getLatitude ( ) + "" , Toast . LENGTH_SHORT ) . show ( ) ;
Log . v ( "TAG" , "获取上次的位置-经纬度:" + location . getLongitude ( ) + " " + location . getLatitude ( ) ) ;
city = getAddress ( location ) ;
public LocationListener locationListener = new LocationListener ( ) {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged ( String provider , int status , Bundle extras ) {
}
// Provider被enable时触发此函数, 比如GPS被打开
@Override
public void onProviderEnabled ( String provider ) {
}
// Provider被disable时触发此函数, 比如GPS被关闭
@Override
public void onProviderDisabled ( String provider ) {
}
//当坐标改变时触发此函数, 如果Provider传进相同的坐标, 它就不会被触发
@Override
public void onLocationChanged ( Location location ) {
if ( location ! = null ) {
//如果位置发生变化,重新显示地理位置经纬度
Toast . makeText ( NoteEditActivity . this , location . getLongitude ( ) + " " +
location . getLatitude ( ) + "" , Toast . LENGTH_SHORT ) . show ( ) ;
Log . v ( "TAG" , "监视地理位置变化-经纬度:" + location . getLongitude ( ) + " " + location . getLatitude ( ) ) ;
}
} else {
//监视地理位置变化, 第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace
locationManager . requestLocationUpdates ( locationProvider , 3000 , 1 , locationListener ) ;
}
} ;
}
return city ;
}
public LocationListener locationListener = new LocationListener ( ) {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onRequestPermissionsResult ( int requestCode , @NonNull String [ ] permissions , @NonNull int [ ] grantResults ) {
switch ( requestCode ) {
case LOCATION_CODE :
if ( grantResults . length > 0 & & grantResults [ 0 ] = = getPackageManager ( ) . PERMISSION_GRANTED
& & grantResults [ 1 ] = = PackageManager . PERMISSION_GRANTED ) {
Toast . makeText ( this , "申请权限" , Toast . LENGTH_LONG ) . show ( ) ;
try {
List < String > providers = locationManager . getProviders ( true ) ;
if ( providers . contains ( LocationManager . NETWORK_PROVIDER ) ) {
//如果是Network
locationProvider = LocationManager . NETWORK_PROVIDER ;
} else if ( providers . contains ( LocationManager . GPS_PROVIDER ) ) {
//如果是GPS
locationProvider = LocationManager . GPS_PROVIDER ;
}
Location location = locationManager . getLastKnownLocation ( locationProvider ) ;
if ( location ! = null ) {
Toast . makeText ( this , location . getLongitude ( ) + " " +
location . getLatitude ( ) + "" , Toast . LENGTH_SHORT ) . show ( ) ;
Log . v ( "TAG" , "获取上次的位置-经纬度:" + location . getLongitude ( ) + " " + location . getLatitude ( ) ) ;
} else {
// 监视地理位置变化, 第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace
locationManager . requestLocationUpdates ( locationProvider , 0 , 0 , locationListener ) ;
}
public void onStatusChanged ( String provider , int status , Bundle extras ) {
}
} catch ( SecurityException e ) {
e . printStackTrace ( ) ;
}
} else {
Toast . makeText ( this , "缺少权限" , Toast . LENGTH_LONG ) . show ( ) ;
finish ( ) ;
}
break ;
}
// Provider被enable时触发此函数, 比如GPS被打开
@Override
public void onProviderEnabled ( String provider ) {
}
//获取地址信息:城市、街道等信息
private String getAddress ( Location location ) {
List < Address > result = null ;
String city = null ;
try {
if ( location ! = null ) {
Geocoder gc = new Geocoder ( this , Locale . getDefault ( ) ) ;
result = gc . getFromLocation ( location . getLatitude ( ) ,
location . getLongitude ( ) , 1 ) ;
city = result . get ( 0 ) . getAddressLine ( 0 ) . toString ( ) ;
Toast . makeText ( this , "获取地址信息:" + city , Toast . LENGTH_LONG ) . show ( ) ;
Log . v ( "TAG" , "获取地址信息:" + city ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
Log . e ( TAG , city ) ;
return city ;
// Provider被disable时触发此函数, 比如GPS被关闭
@Override
public void onProviderDisabled ( String provider ) {
}
//当坐标改变时触发此函数, 如果Provider传进相同的坐标, 它就不会被触发
@Override
public void onLocationChanged ( Location location ) {
if ( location ! = null ) {
//如果位置发生变化,重新显示地理位置经纬度
Toast . makeText ( NoteEditActivity . this , location . getLongitude ( ) + " " +
location . getLatitude ( ) + "" , Toast . LENGTH_SHORT ) . show ( ) ;
Log . v ( "TAG" , "监视地理位置变化-经纬度:" + location . getLongitude ( ) + " " + location . getLatitude ( ) ) ;
}
}
} ;
@Override
public void onRequestPermissionsResult ( int requestCode , @NonNull String [ ] permissions , @NonNull int [ ] grantResults ) {
switch ( requestCode ) {
case LOCATION_CODE :
if ( grantResults . length > 0 & & grantResults [ 0 ] = = getPackageManager ( ) . PERMISSION_GRANTED
& & grantResults [ 1 ] = = PackageManager . PERMISSION_GRANTED ) {
Toast . makeText ( this , "申请权限" , Toast . LENGTH_LONG ) . show ( ) ;
try {
List < String > providers = locationManager . getProviders ( true ) ;
if ( providers . contains ( LocationManager . NETWORK_PROVIDER ) ) {
//如果是Network
locationProvider = LocationManager . NETWORK_PROVIDER ;
} else if ( providers . contains ( LocationManager . GPS_PROVIDER ) ) {
//如果是GPS
locationProvider = LocationManager . GPS_PROVIDER ;
}
Location location = locationManager . getLastKnownLocation ( locationProvider ) ;
if ( location ! = null ) {
Toast . makeText ( this , location . getLongitude ( ) + " " +
location . getLatitude ( ) + "" , Toast . LENGTH_SHORT ) . show ( ) ;
Log . v ( "TAG" , "获取上次的位置-经纬度:" + location . getLongitude ( ) + " " + location . getLatitude ( ) ) ;
} else {
// 监视地理位置变化, 第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace
locationManager . requestLocationUpdates ( locationProvider , 0 , 0 , locationListener ) ;
}
} catch ( SecurityException e ) {
e . printStackTrace ( ) ;
}
} else {
Toast . makeText ( this , "缺少权限" , Toast . LENGTH_LONG ) . show ( ) ;
finish ( ) ;
}
break ;
}
}
//获取地址信息:城市、街道等信息
private String getAddress ( Location location ) {
List < Address > result = null ;
String city = null ;
try {
if ( location ! = null ) {
Geocoder gc = new Geocoder ( this , Locale . getDefault ( ) ) ;
result = gc . getFromLocation ( location . getLatitude ( ) ,
location . getLongitude ( ) , 1 ) ;
city = result . get ( 0 ) . getAddressLine ( 0 ) . toString ( ) ;
Toast . makeText ( this , "获取地址信息:" + city , Toast . LENGTH_LONG ) . show ( ) ;
Log . v ( "TAG" , "获取地址信息:" + city ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
Log . e ( TAG , city ) ;
return city ;
}
private void local_sel ( ) {
private void local_sel ( ) {
final Button get_local = findViewById ( R . id . location ) ;
get_local . setOnClickListener ( new OnClickListener ( ) {
@Override
@ -1188,9 +1226,9 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
String city = null ;
city = getLocation ( ) ;
if ( city = = null ) {
if ( city = = null ) {
get_local . setText ( "点击获得地理信息" ) ;
} else {
} else {
get_local . setText ( city ) ;
}
}
@ -1212,7 +1250,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
} ) ;
}
public void speech_sum ( ) {
public void speech_sum ( ) {
EditText editable = findViewById ( R . id . note_edit_view ) ;
//Toast.makeText(NoteEditActivity.this, editable.getText().toString(), Toast.LENGTH_SHORT).show();
final Button speak = findViewById ( R . id . iatBtn ) ;
@ -1228,26 +1266,28 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
@Override
public void beforeTextChanged ( CharSequence s , int start , int count , int after ) {
}
@Override
public void onTextChanged ( CharSequence s , int start , int before , int count ) {
tmp [ 0 ] = cutOfimage ( operateText ( s . toString ( ) ) ) . length ( ) ;
TextView number_word = findViewById ( R . id . num_word ) ;
number_word . setText ( "字符数:" + tmp [ 0 ] ) ;
number_word . setText ( "字符数:" + tmp [ 0 ] ) ;
final String temp = cutOfimage ( s . toString ( ) ) ;
speak . setOnClickListener ( new OnClickListener ( ) {
@Override
public void onClick ( View v ) {
tts [ 0 ] . speak ( temp . toString ( ) , TextToSpeech . QUEUE_FLUSH , null ) ;
tts [ 0 ] . speak ( temp . toString ( ) , TextToSpeech . QUEUE_FLUSH , null ) ;
}
} ) ;
}
@Override
public void afterTextChanged ( Editable s ) {
}
} ) ;
}
public String operateText ( String str ) {
public String operateText ( String str ) {
String dest = "" ;
Pattern p = Pattern . compile ( "\\s*|t|r|n" ) ;
Matcher m = p . matcher ( str ) ;
@ -1255,40 +1295,39 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
return dest ;
}
public String cutOfimage ( String str ) {
String dest = str ;
int index1 = dest . indexOf ( "[local]" ) ;
int index2 = dest . indexOf ( "[/local]" ) ;
while ( index1 ! = - 1 & & index2 ! = - 1 ) {
dest = dest . substring ( 0 , index1 ) + dest . substring ( index2 + 8 ) ;
index1 = dest . indexOf ( "[local]" ) ;
index2 = dest . indexOf ( "[/local]" ) ;
}
return dest ;
public String cutOfimage ( String str ) {
String dest = str ;
int index1 = dest . indexOf ( "[local]" ) ;
int index2 = dest . indexOf ( "[/local]" ) ;
while ( index1 ! = - 1 & & index2 ! = - 1 ) {
dest = dest . substring ( 0 , index1 ) + dest . substring ( index2 + 8 ) ;
index1 = dest . indexOf ( "[local]" ) ;
index2 = dest . indexOf ( "[/local]" ) ;
}
return dest ;
}
private void convertToImage ( ) {
NoteEditText noteEditText = ( NoteEditText ) findViewById ( R . id . note_edit_view ) ;
Editable editable = noteEditText . getText ( ) ;
String noteText = editable . toString ( ) ;
int length = editable . length ( ) ;
for ( int i = 0 ; i < length ; i + + ) {
for ( int j = i ; j < length ; j + + ) {
String img_fragment = noteText . substring ( i , j + 1 ) ;
if ( img_fragment . length ( ) > 15 & & img_fragment . endsWith ( "[/local]" ) & & img_fragment . startsWith ( "[local]" ) ) {
for ( int i = 0 ; i < length ; i + + ) {
for ( int j = i ; j < length ; j + + ) {
String img_fragment = noteText . substring ( i , j + 1 ) ;
if ( img_fragment . length ( ) > 15 & & img_fragment . endsWith ( "[/local]" ) & & img_fragment . startsWith ( "[local]" ) ) {
int limit = 7 ;
int len = img_fragment . length ( ) - 15 ;
String path = img_fragment . substring ( limit , limit + len ) ;
int len = img_fragment . length ( ) - 15 ;
String path = img_fragment . substring ( limit , limit + len ) ;
Bitmap bitmap = null ;
Log . d ( TAG , "图片的路径是:" + path ) ;
Log . d ( TAG , "图片的路径是:" + path ) ;
try {
bitmap = BitmapFactory . decodeFile ( path ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
if ( bitmap ! = null ) {
if ( bitmap ! = null ) {
Log . d ( TAG , "图片不为null" ) ;
ImageSpan imageSpan = new ImageSpan ( NoteEditActivity . this , bitmap ) ;
String ss = "[local]" + path + "[/local]" ;
@ -1296,7 +1335,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
spannableString . setSpan ( imageSpan , 0 , ss . length ( ) , Spannable . SPAN_EXCLUSIVE_EXCLUSIVE ) ;
Log . d ( TAG , "Create spannable string success!" ) ;
Editable edit_text = noteEditText . getEditableText ( ) ;
edit_text . delete ( i , i + len + 15 ) ;
edit_text . delete ( i , i + len + 15 ) ;
edit_text . insert ( i , spannableString ) ;
}
}
@ -1305,7 +1344,6 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
}
@RequiresApi ( api = Build . VERSION_CODES . KITKAT )
protected void onActivityResult ( int requestCode , int resultCode , Intent intent ) {
super . onActivityResult ( requestCode , resultCode , intent ) ;
@ -1321,11 +1359,11 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
e . printStackTrace ( ) ;
}
if ( bitmap ! = null ) {
if ( bitmap ! = null ) {
Log . d ( TAG , "onActivityResult: bitmap is not null" ) ;
ImageSpan imageSpan = new ImageSpan ( NoteEditActivity . this , bitmap ) ;
String path = getPath ( this , originalUri ) ;
String img_fragment = "[local]" + path + "[/local]" ;
String path = getPath ( this , originalUri ) ;
String img_fragment = "[local]" + path + "[/local]" ;
SpannableString spannableString = new SpannableString ( img_fragment ) ;
spannableString . setSpan ( imageSpan , 0 , img_fragment . length ( ) , Spannable . SPAN_EXCLUSIVE_EXCLUSIVE ) ;
NoteEditText e = ( NoteEditText ) findViewById ( R . id . note_edit_view ) ;
@ -1337,13 +1375,13 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
ContentResolver contentResolver = getContentResolver ( ) ;
ContentValues contentValues = new ContentValues ( ) ;
final long id = mWorkingNote . getNoteId ( ) ;
contentValues . put ( "snippet" , mWorkingNote . getContent ( ) ) ;
contentResolver . update ( Uri . parse ( "content://micode_notes/note" ) , contentValues , "_id=?" , new String [ ] { "" + id } ) ;
contentValues . put ( "snippet" , mWorkingNote . getContent ( ) ) ;
contentResolver . update ( Uri . parse ( "content://micode_notes/note" ) , contentValues , "_id=?" , new String [ ] { "" + id } ) ;
ContentValues contentValues1 = new ContentValues ( ) ;
contentValues1 . put ( "content" , mWorkingNote . getContent ( ) ) ;
contentResolver . update ( Uri . parse ( "content://micode_notes/data" ) , contentValues1 , "mime_type=? and note_id=?" , new String [ ] { "vnd.android.cursor.item/text_note" , "" + id } ) ;
contentValues1 . put ( "content" , mWorkingNote . getContent ( ) ) ;
contentResolver . update ( Uri . parse ( "content://micode_notes/data" ) , contentValues1 , "mime_type=? and note_id=?" , new String [ ] { "vnd.android.cursor.item/text_note" , "" + id } ) ;
} else {
} else {
Toast . makeText ( NoteEditActivity . this , "获取图片失败" , Toast . LENGTH_SHORT ) . show ( ) ;
}
break ;
@ -1409,8 +1447,8 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
}
public void showSingleAlertDiglog ( ) {
final String [ ] items = { "默认-普通" , "默认-非衬线" , "默认-衬线" , "默认-等宽" , "仿宋" , "黑体" , "楷体" , "姚体" , "隶书" , "行楷" , "新魏" , "中宋" } ;
public void showSingleAlertDiglog ( ) {
final String [ ] items = { "默认-普通" , "默认-非衬线" , "默认-衬线" , "默认-等宽" , "仿宋" , "黑体" , "楷体" , "姚体" , "隶书" , "行楷" , "新魏" , "中宋" } ;
AlertDialog . Builder alertBuilder = new AlertDialog . Builder ( this ) ;
alertBuilder . setTitle ( "选择字体" ) ;
alertBuilder . setSingleChoiceItems ( items , 0 , new DialogInterface . OnClickListener ( ) {
@ -1465,7 +1503,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
Toast . makeText ( NoteEditActivity . this , items [ i ] , Toast . LENGTH_SHORT ) . show ( ) ;
}
} ) ;
alertBuilder . setPositiveButton ( "确定" , new DialogInterface . OnClickListener ( ) {
alertBuilder . setPositiveButton ( "确定" , new DialogInterface . OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialogInterface , int i ) {
alertDialog2 . dismiss ( ) ;
@ -1484,7 +1522,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
alertDialog2 . show ( ) ;
}
public void translate ( ) {
public void translate ( ) {
final EditText editable = findViewById ( R . id . note_edit_view ) ;
final Button get_local = findViewById ( R . id . translate ) ;
get_local . setOnClickListener ( new OnClickListener ( ) {
@ -1525,9 +1563,9 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
trans3 . setOnClickListener ( new OnClickListener ( ) {
@Override
public void onClick ( View v ) {
if ( restore_translate = = null | | restore_translate . toString ( ) . equals ( editable . getText ( ) . toString ( ) ) ) {
if ( restore_translate = = null | | restore_translate . toString ( ) . equals ( editable . getText ( ) . toString ( ) ) ) {
Toast . makeText ( NoteEditActivity . this , "无可还原内容" , Toast . LENGTH_SHORT ) . show ( ) ;
} else {
} else {
editable . setText ( restore_translate ) ;
Toast . makeText ( NoteEditActivity . this , "已还原" , Toast . LENGTH_SHORT ) . show ( ) ;
}
@ -1541,7 +1579,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
public void translate_z2u ( ) {
final EditText editable = findViewById ( R . id . note_edit_view ) ;
String word = editable . getText ( ) . toString ( ) ;
word = word . replaceAll ( "\\n" , "//" ) ;
word = word . replaceAll ( "\\n" , "//" ) ;
String from = "auto" ;
String to = "en" ;
String appid = "20221021001406789" ;
@ -1555,7 +1593,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
. baseUrl ( "https://fanyi-api.baidu.com/api/trans/vip/" )
. addConverterFactory ( GsonConverterFactory . create ( ) )
. build ( ) ;
BaiduTranslateService baiduTranslateService = retrofitBaidu . create ( BaiduTranslateService . class ) ;
BaiduTranslateService baiduTranslateService = retrofitBaidu . create ( BaiduTranslateService . class ) ;
retrofit2 . Call < RespondBean > call = baiduTranslateService . translate ( word , from , to , appid , salt , sign ) ;
call . enqueue ( new Callback < RespondBean > ( ) {
@Override
@ -1566,6 +1604,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
editable . setText ( result ) ;
Log . d ( TAG , "中译英结果" + result ) ;
}
@Override
public void onFailure ( retrofit2 . Call < RespondBean > call , Throwable t ) {
Log . d ( TAG , "onResponse: 请求失败 " + t ) ;
@ -1576,7 +1615,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
public void translate_u2z ( ) {
final EditText editable = findViewById ( R . id . note_edit_view ) ;
String word = editable . getText ( ) . toString ( ) ;
word = word . replaceAll ( "\\n" , "//" ) ;
word = word . replaceAll ( "\\n" , "//" ) ;
Log . d ( TAG , word ) ;
String from = "auto" ;
String to = "zh" ;
@ -1591,7 +1630,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
. baseUrl ( "https://fanyi-api.baidu.com/api/trans/vip/" )
. addConverterFactory ( GsonConverterFactory . create ( ) )
. build ( ) ;
BaiduTranslateService baiduTranslateService = retrofitBaidu . create ( BaiduTranslateService . class ) ;
BaiduTranslateService baiduTranslateService = retrofitBaidu . create ( BaiduTranslateService . class ) ;
retrofit2 . Call < RespondBean > call = baiduTranslateService . translate ( word , from , to , appid , salt , sign ) ;
call . enqueue ( new Callback < RespondBean > ( ) {
@Override
@ -1602,6 +1641,7 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
editable . setText ( result ) ;
Log . d ( TAG , "中译英结果" + result ) ;
}
@Override
public void onFailure ( retrofit2 . Call < RespondBean > call , Throwable t ) {
Log . d ( TAG , "onResponse: 请求失败 " + t ) ;
@ -1609,9 +1649,41 @@ public class NoteEditActivity<关闭> extends AppCompatActivity implements OnCli
} ) ;
}
private static final int MAX_TIME_OF_RVOKE_TIME = 100 ;
private int MAX_OF_RVOKE_TIME = 100 ;
private Vector < SpannableString > mChanged = new Vector < SpannableString > ( MAX_OF_RVOKE_TIME ) ;
private void saveMyChanged ( ) {
SpannableString text = new SpannableString ( mNoteEditor . getText ( ) ) ; //用getText方法获取每次编辑的内容
if ( mChanged . size ( ) > = MAX_TIME_OF_RVOKE_TIME ) { //如果栈中的数据大于最大撤销次数,就把第一次修改的内容删除
mChanged . removeElementAt ( 0 ) ;
}
mChanged . add ( text ) ; //然后把本次修改的内容加入栈中
}
}
private void doRevoke ( ) {
int size = mChanged . size ( ) ; //获取当前栈大小
AlertDialog . Builder dialog = new AlertDialog . Builder ( this ) ; //创建一个alertdialog窗口
dialog . setTitle ( R . string . tips_of_revoke ) ; //设置title信息
dialog . setCancelable ( true ) ; //设置为可取消
dialog . setPositiveButton ( "OK" , new DialogInterface . OnClickListener ( ) { //只需要设置一个OK键即可
@Override
public void onClick ( DialogInterface dialog , int which ) {
}
} ) ;
mIsRvoke = true ; //把是否已执行撤销的标记设置为true
if ( size < = 1 ) { //如果栈中元素过少,打印提示信息
dialog . setMessage ( R . string . have_not_input_anything ) ; //提示用户您还没有输入任何信息
dialog . show ( ) ; //显示当前alertdialog
return ;
} else {
mNoteEditor . setText ( ( CharSequence ) mChanged . elementAt ( size - 2 ) ) ; //在textview中设置撤销的内容
mNoteEditor . setSelection ( mNoteEditor . length ( ) ) ;
mChanged . removeElementAt ( size - 1 ) ; //删除元素
if ( size = = 2 ) {
dialog . setMessage ( R . string . can_not_revoke ) ; //如果只有一次操作,那么提示用户不能再撤销了
dialog . show ( ) ; //显示当前alertdialog
}
}
}
}