HashMap deep think

1 举栗子

先来复习一下我们常用的几个方法

[java]  view plain  copy
  1. public class HashMapTest {  
  2.   
  3.     public static void main(String[] args) {  
  4.         // TODO Auto-generated method stub  
  5.         HashMap<String, String> hashMap=new HashMap<>();  
  6.         //添加方法  
  7.         hashMap.put("1""chris");  
  8.                //遍历方法1_for  
  9.         Set<String> keys=hashMap.keySet();  
  10.         for(String key:keys){  
  11.             System.out.println(key+"="+hashMap.get(key));  
  12.         }  
  13.         //遍历方法1_iterator(for和iterator实现原理相同)  
  14.                 Iterator iter = map.keySet().iterator();   
  15.                 while (iter.hasNext()) {   
  16.                 String key = iter.next();   
  17.                 String value = map.get(key);   
  18.                 }   
  19.         //遍历方法2_for  
  20.                 Set<Entry<String, String>> entrys= hashMap.entrySet();  
  21.         for(Entry<String, String> entry:entrys){  
  22.             String key=entry.getKey();  
  23.             String value=entry.getValue();  
  24.         }  
  25.         //遍历方法2_iterator  
  26.         Iterator<Entry<String, String>> iterator=hashMap.entrySet().iterator();  
  27.         while(iterator.hasNext()){  
  28.             Map.Entry<String, String> entry=iterator.next();  
  29.             String key=entry.getKey();  
  30.             String value=entry.getValue();  
  31.         }  
  32.         //查询方法  
  33.         hashMap.get("1");  
  34.         //删除方法  
  35.         hashMap.remove("1");          
  36.     }  
  37.   
  38. }  

2 HashMap类图结构



3 HashMap数据结构


我们知道在Java中最常用的两种结构是数组和模拟指针(引用),几乎所有的数据结构都可以利用这两种来组合实现。数组的存储方式在内存的地址是连续的,大小固定,一旦分配不能被其他引用占用。它的特点是查询快,时间复杂度是O(1),插入和删除的操作比较慢,时间复杂度是O(n),链表的存储方式是非连续的,大小不固定,特点与数组相反,插入和删除快,查询速度慢。HashMap可以说是一种折中的方案吧。


4 HashMap重要概念




5 HashMap源码分析

老规矩,按照使用的顺序来分析源码

1.HashMap<String, String> hashMap=new HashMap<>();

[java]  view plain  copy
  1. public HashMap() {  
  2.         this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);  
  3.     }  
其中默认容量DEFAULT_INITIAL_CAPACITY

[java]  view plain  copy
  1. static final int DEFAULT_INITIAL_CAPACITY = 4;//android N  
默认加载因子DEFAULT_LOAD_FACTOR
[java]  view plain  copy
  1. static final float DEFAULT_LOAD_FACTOR = 0.75f;//android N  
构造函数有几个,但最后都会落到HashMap(int initialCapacity, float loadFactor)

[java]  view plain  copy
  1. public HashMap(int initialCapacity, float loadFactor) {    
  2.         //初始容量不能<0    
  3.         if (initialCapacity < 0)    
  4.             throw new IllegalArgumentException("Illegal initial capacity: "    
  5.                     + initialCapacity);    
  6.         //初始容量不能 > 最大容量值,HashMap的最大容量值为2^30    
  7.         if (initialCapacity > MAXIMUM_CAPACITY)    
  8.             initialCapacity = MAXIMUM_CAPACITY;    
  9.         //负载因子不能 < 0    
  10.         if (loadFactor <= 0 || Float.isNaN(loadFactor))    
  11.             throw new IllegalArgumentException("Illegal load factor: "    
  12.                     + loadFactor);    
  13.     
  14.         // 计算出大于 initialCapacity 的最小的 2 的 n 次方值。    
  15.         int capacity = 1;    
  16.         while (capacity < initialCapacity)    
  17.             capacity <<= 1;    
  18.             
  19.         this.loadFactor = loadFactor;    
  20.         //设置HashMap的容量极限,当HashMap的容量达到该极限时就会进行扩容操作    
  21.         threshold = (int) (capacity * loadFactor);    
  22.         //初始化table数组    
  23.         table = new Entry[capacity];    
  24.         init();    
  25.     }  
其中涉及到位运算<<,,capacity <<= 1等价于capacity=capacity<<1,表示capacity左移1位
从源码中可以看出,每次新建一个HashMap时,都会初始化一个table数组。table数组的元素为Entry节点

[java]  view plain  copy
  1. static class Entry<K,V> implements Map.Entry<K,V> {    
  2.         final K key;    
  3.         V value;    
  4.         Entry<K,V> next;    
  5.         final int hash;    
  6.     
  7.         /**  
  8.          * Creates new entry.  
  9.          */    
  10.         Entry(int h, K k, V v, Entry<K,V> n) {    
  11.             value = v;    
  12.             next = n;    
  13.             key = k;    
  14.             hash = h;    
  15.         }    
  16.         .......    
  17.     }  
其中Entry为HashMap的内部类,它包含了键key、值value、下一个节点next,以及hash值,这是非常重要的,正是由于Entry才构成了table数组的项为链表

2.hashMap.put("1", "chris");

先来看看put的几种分支



HashMap通过键的hashCode来快速的存取元素。当不同的对象hashCode发生碰撞时,HashMap通过单链表来解决,将新元素加入链表表头,通过next指向原有的元素。

先说说大概的过程:当我们调用put存值时,HashMap首先会获取key的哈希值,通过哈希值快速找到某个存放位置,这个位置可以被称之为bucketIndex。

对于一个key,如果hashCode不同,equals一定为false,如果hashCode相同,equals不一定为true。

所以理论上,hashCode可能存在冲突的情况,也叫发生了碰撞,当碰撞发生时,计算出的bucketIndex也是相同的,这时会取到bucketIndex位置已存储的元素,最终通过equals来比较,equals方法就是哈希码碰撞时才会执行的方法,所以说HashMap很少会用到equals。HashMap通过hashCode和equals最终判断出K是否已存在,如果已存在,则使用新V值替换旧V值,并返回旧V值,如果不存在 ,则存放新的键值对<K, V>到bucketIndex位置。

下面我们来看看put的源码

[java]  view plain  copy
  1. public V put(K key, V value) {    
  2.         //当key为null,调用putForNullKey方法,保存null于table第一个位置中,这是HashMap允许为null的原因    
  3.         if (key == null)    
  4.             return putForNullKey(value);    
  5.         //计算key的hash值    
  6.         int hash = hash(key.hashCode());                  ------(1)    
  7.         //计算key hash 值在 table 数组中的位置    
  8.         int i = <span style="font-family:Arial, Helvetica, sans-serif;">indexFor(hash, table.length)</span><span style="font-family:Arial, Helvetica, sans-serif;">;             ------(2)  </span>  
  9.         //从i出开始迭代 e,找到 key 保存的位置    
  10.         for (Entry<K, V> e = table[i]; e != null; e = e.next) {    
  11.             Object k;    
  12.             //判断该条链上是否有hash值相同的(key相同)    
  13.             //若存在相同,则直接覆盖value,返回旧value    
  14.             if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {    
  15.                 V oldValue = e.value;    //旧值 = 新值    
  16.                 e.value = value;    
  17.                 e.recordAccess(this);    
  18.                 return oldValue;     //返回旧值    
  19.             }    
  20.         }    
  21.         //修改次数增加1    
  22.         modCount++;    
  23.         //将key、value添加至i位置处    
  24.         addEntry(hash, key, value, i);    
  25.         return null;    
  26.     }  
通过源码我们可以清晰看到HashMap保存数据的过程为:

1)首先判断key是否为null,若为null,则直接调用putForNullKey方法

[java]  view plain  copy
  1. private V putForNullKey(V value) {  
  2.         for (HashMapEntry<K,V> e = table[0]; e != null; e = e.next) {  
  3.             if (e.key == null) {  
  4.                 V oldValue = e.value;  
  5.                 e.value = value;  
  6.                 e.recordAccess(this);  
  7.                 return oldValue;  
  8.             }  
  9.         }  
  10.         modCount++;  
  11.         addEntry(0null, value, 0);  
  12.         return null;  
  13.     }  

从代码可以看出,如果key为null的值,默认就存储到table[0]开头的链表了。然后遍历table[0]的链表的每个节点Entry,如果发现其中存在节点Entry的key为null,就替换新的value,然后返回旧的value,如果没发现key等于null的节点Entry,就增加新的节点


2)计算key的hashcode(hash(key.hashCode())),再用计算的结果二次hash(indexFor(hash, table.length)),找到Entry数组的索引i,这里涉及到hash算法,最后会详细讲解


3)遍历以table[i]为头节点的链表,如果发现hash,key都相同的节点时,就替换为新的value,然后返回旧的value,只有hash相同时,循环内并没有做任何处理


4)modCount++代表修改次数,与迭代相关,在迭代篇会详细讲解


5)对于hash相同但key不相同的节点以及hash不相同的节点,就增加新的节点(addEntry())

[java]  view plain  copy
  1. void addEntry(int hash, K key, V value, int bucketIndex) {    
  2.         //获取bucketIndex处的Entry    
  3.         Entry<K, V> e = table[bucketIndex];    
  4.         //将新创建的 Entry 放入 bucketIndex 索引处,并让新的 Entry 指向原来的 Entry     
  5.         table[bucketIndex] = new Entry<K, V>(hash, key, value, e);    
  6.         //若HashMap中元素的个数超过极限了,则容量扩大两倍    
  7.         if (size++ >= threshold)    
  8.             resize(2 * table.length);    
  9.     }   
这里新增加节点采用了头插法,新节点都增加到头部,新节点的next指向老节点

这里涉及到了HashMap的扩容问题,随着HashMap中元素的数量越来越多,发生碰撞的概率就越来越大,所产生的链表长度就会越来越长,这样势必会影响HashMap的速度,为了保证HashMap的效率,系统必须要在某个临界点进行扩容处理。该临界点在当HashMap中元素的数量等于table数组长度*加载因子。但是扩容是一个非常耗时的过程,因为它需要重新计算这些数据在新table数组中的位置并进行复制处理。

[java]  view plain  copy
  1. void resize(int newCapacity) {  
  2.         HashMapEntry[] oldTable = table;  
  3.         int oldCapacity = oldTable.length;  
  4.         if (oldCapacity == MAXIMUM_CAPACITY) {  
  5.             threshold = Integer.MAX_VALUE;  
  6.             return;  
  7.         }  
  8.   
  9.         HashMapEntry[] newTable = new HashMapEntry[newCapacity];  
  10.         transfer(newTable);  
  11.         table = newTable;  
  12.         threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);  
  13.     }  
从代码可以看出,如果大小超过最大容量就返回。否则就new 一个新的Entry数组,长度为旧的Entry数组长度的两倍。然后将旧的Entry[]复制到新的Entry[].

[java]  view plain  copy
  1. void transfer(HashMapEntry[] newTable) {  
  2.         int newCapacity = newTable.length;  
  3.         for (HashMapEntry<K,V> e : table) {  
  4.             while(null != e) {  
  5.                 HashMapEntry<K,V> next = e.next;  
  6.                 int i = indexFor(e.hash, newCapacity);  
  7.                 e.next = newTable[i];  
  8.                 newTable[i] = e;  
  9.                 e = next;  
  10.             }  
  11.         }  
  12.     }  

在复制的时候数组的索引int i = indexFor(e.hash, newCapacity);重新参与计算


3.Iterator iter = map.keySet().iterator();

keySet()方法可以获取包含key的set集合,调用该集合的迭代器可以对key值遍历

[java]  view plain  copy
  1. public Set<K> keySet() {  
  2.         Set<K> ks = keySet;  
  3.         if (ks == null) {  
  4.             ks = new KeySet();  
  5.             keySet = ks;  
  6.         }  
  7.         return ks;  
  8.     }  

KeySet是HashMap中的内部类,继承AbstractSet,KeySet中获取的迭代器为KeyIterator

[java]  view plain  copy
  1. private final class KeySet extends AbstractSet<K> {  
  2.         public Iterator<K> iterator() {  
  3.             return new KeyIterator();  
  4.         }  
  5.         ......  
  6.     }  
KeyIterator继承自HashIterator
[java]  view plain  copy
  1. private final class KeyIterator extends HashIterator<K> {  
  2.         public K next() {  
  3.             return nextEntry().getKey();  
  4.         }  
  5.     }  
[java]  view plain  copy
  1. private abstract class HashIterator<E> implements Iterator<E> {  
  2.         HashMapEntry<K,V> next;        // next entry to return  
  3.         int expectedModCount;   // For fast-fail  
  4.         int index;              // current slot  
  5.         HashMapEntry<K,V> current;     // current entry  
  6.   
  7.         HashIterator() {  
  8.             expectedModCount = modCount;  
  9.             if (size > 0) { // advance to first entry  
  10.                 HashMapEntry[] t = table;  
  11.                 while (index < t.length && (next = t[index++]) == null)  
  12.                     ;  
  13.             }  
  14.         }  
  15.   
  16.         public final boolean hasNext() {  
  17.             return next != null;  
  18.         }  
  19.   
  20.         final Entry<K,V> nextEntry() {  
  21.             if (modCount != expectedModCount)  
  22.                 throw new ConcurrentModificationException();  
  23.             HashMapEntry<K,V> e = next;  
  24.             if (e == null)  
  25.                 throw new NoSuchElementException();  
  26.   
  27.             if ((next = e.next) == null) {  
  28.                 HashMapEntry[] t = table;  
  29.                 while (index < t.length && (next = t[index++]) == null)  
  30.                     ;  
  31.             }  
  32.             current = e;  
  33.             return e;  
  34.         }  
  35.   
  36.         public void remove() {  
  37.             if (current == null)  
  38.                 throw new IllegalStateException();  
  39.             if (modCount != expectedModCount)  
  40.                 throw new ConcurrentModificationException();  
  41.             Object k = current.key;  
  42.             current = null;  
  43.             HashMap.this.removeEntryForKey(k);  
  44.             expectedModCount = modCount;  
  45.         }  
  46.     }  

4.Iterator<Entry<String, String>> iterator=hashMap.entrySet().iterator();
[java]  view plain  copy
  1. public Set<Map.Entry<K,V>> entrySet() {  
  2.         return entrySet0();  
  3.     }  
[java]  view plain  copy
  1. private Set<Map.Entry<K,V>> entrySet0() {  
  2.         Set<Map.Entry<K,V>> es = entrySet;  
  3.         return es != null ? es : (entrySet = new EntrySet());  
  4.     }  
EntrySet是HashMap内部类,继承AbstractSet,EntrySet中获取的迭代器为EntryIterator

[java]  view plain  copy
  1. private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {  
  2.         public Iterator<Map.Entry<K,V>> iterator() {  
  3.             return newEntryIterator();  
  4.         }        ......  
  5.     }  
[java]  view plain  copy
  1. Iterator<Map.Entry<K,V>> newEntryIterator()   {  
  2.         return new EntryIterator();  
  3.     }  
[java]  view plain  copy
  1. private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {  
  2.         public Map.Entry<K,V> next() {  
  3.             return nextEntry();  
  4.         }  
  5.     }  
[java]  view plain  copy
  1. private abstract class HashIterator<E> implements Iterator<E> {  
  2.         HashMapEntry<K,V> next;        // next entry to return  
  3.         int expectedModCount;   // For fast-fail  
  4.         int index;              // current slot  
  5.         HashMapEntry<K,V> current;     // current entry  
  6.   
  7.         HashIterator() {  
  8.             expectedModCount = modCount;  
  9.             if (size > 0) { // advance to first entry  
  10.                 HashMapEntry[] t = table;  
  11.                 while (index < t.length && (next = t[index++]) == null)  
  12.                     ;  
  13.             }  
  14.         }  
  15.   
  16.         public final boolean hasNext() {  
  17.             return next != null;  
  18.         }  
  19.   
  20.         final Entry<K,V> nextEntry() {  
  21.             if (modCount != expectedModCount)  
  22.                 throw new ConcurrentModificationException();  
  23.             HashMapEntry<K,V> e = next;  
  24.             if (e == null)  
  25.                 throw new NoSuchElementException();  
  26.   
  27.             if ((next = e.next) == null) {  
  28.                 HashMapEntry[] t = table;  
  29.                 while (index < t.length && (next = t[index++]) == null)  
  30.                     ;  
  31.             }  
  32.             current = e;  
  33.             return e;  
  34.         }  
  35.   
  36.         public void remove() {  
  37.             if (current == null)  
  38.                 throw new IllegalStateException();  
  39.             if (modCount != expectedModCount)  
  40.                 throw new ConcurrentModificationException();  
  41.             Object k = current.key;  
  42.             current = null;  
  43.             HashMap.this.removeEntryForKey(k);  
  44.             expectedModCount = modCount;  
  45.         }  
  46.     }  
显然entrySet()遍历的效率会比keySet()高,因为keySet获取key的集合后,还需要调用get()方法,相当于遍历两次

5.hashMap.get("1");

[java]  view plain  copy
  1. public V get(Object key) {    
  2.         // 若为null,调用getForNullKey方法返回相对应的value    
  3.         if (key == null)    
  4.             return getForNullKey();    
  5.         // 根据该 key 的 hashCode 值计算它的 hash 码      
  6.         int hash = hash(key.hashCode());    
  7.         // 取出 table 数组中指定索引处的值    
  8.         for (Entry<K, V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) {    
  9.             Object k;    
  10.             //若搜索的key与查找的key相同,则返回相对应的value    
  11.             if (e.hash == hash && ((k = e.key) == key || key.equals(k)))    
  12.                 return e.value;    
  13.         }    
  14.         return null;    
  15.     }  

在这里能够根据key快速的取到value除了和HashMap的数据结构密不可分外,还和Entry有莫大的关系,在前面就提到过,HashMap在存储过程中并没有将key,value分开来存储,而是当做一个整体key-value来处理的,这个整体就是Entry对象。同时value也只相当于key的附属而已。在存储的过程中,系统根据key的hashcode来决定Entry在table数组中的存储位置,在取的过程中同样根据key的hashcode取出相对应的Entry对象


6.hashMap.remove("1");

[java]  view plain  copy
  1. public V remove(Object key) {  
  2.         Entry<K,V> e = removeEntryForKey(key);  
  3.         return (e == null ? null : e.getValue());  
  4.     }  

[java]  view plain  copy
  1. final Entry<K,V> removeEntryForKey(Object key) {  
  2.         if (size == 0) {  
  3.             return null;  
  4.         }  
  5.         int hash = (key == null) ? 0 : sun.misc.Hashing.singleWordWangJenkinsHash(key);  
  6.         int i = indexFor(hash, table.length);  
  7.         HashMapEntry<K,V> prev = table[i];  
  8.         HashMapEntry<K,V> e = prev;  
  9.   
  10.         while (e != null) {  
  11.             HashMapEntry<K,V> next = e.next;  
  12.             Object k;  
  13.             if (e.hash == hash &&  
  14.                 ((k = e.key) == key || (key != null && key.equals(k)))) {  
  15.                 modCount++;  
  16.                 size--;  
  17.                 if (prev == e)  
  18.                     table[i] = next;  
  19.                 else  
  20.                     prev.next = next;  
  21.                 e.recordRemoval(this);  
  22.                 return e;  
  23.             }  
  24.             prev = e;  
  25.             e = next;  
  26.         }  
  27.   
  28.         return e;  
  29.     }  


6 总结

1.HashMap结合了数组和链表的优点,使用Hash算法加快访问速度,使用散列表解决碰撞冲突的问题,其中数组的每个元素是单链表的头结点,链表是用来解决冲突的


2.HashMap有两个重要的参数:初始容量和加载因子。这两个参数极大的影响了HashMap的性能。初始容量是hash数组的长度,当前加载因子=当前hash数组元素/hash数组长度,最大加载因子为最大能容纳的数组元素个数(默认最大加载因子为0.75),当hash数组中的元素个数超出了最大加载因子和容量的乘积时,要对hashMap进行扩容,扩容过程存在于hashmap的put方法中,扩容过程始终以2次方增长。


3.HashMap是泛型类,key和value可以为任何类型,包括null类型。key为null的键值对永远都放在以table[0]为头结点的链表中,当然不一定是存放在头结点table[0]中。


4.哈希表的容量一定是2的整数次幂,我们在HashMap算法篇会详细讲解

当这个alias标签=刀具亭才显示这几个字段 <fm:noteRequestReport alias="刀具站"/>,returnTool.add(new ColumnConfig("完全可用", "useableQty", "50px")); returnTool.add(new ColumnConfig("不可用待挑选", "unUseableQty", "80px")); returnTool.add(new ColumnConfig("损坏", "demageQty", "30px")); returnTool.add(new ColumnConfig("打飞", "offQty", "30px"));,代码 public Map<String, List<ColumnConfig>> header() { Map<String, List<ColumnConfig>> headerMap = new HashMap<>(); // 机床配置报表 List<ColumnConfig> config = new ArrayList<>(); config.add(new ColumnConfig("组织", "orgCode", "30px")); config.add(new ColumnConfig("申请单号", "requestNo", "120px")); config.add(new ColumnConfig("刀具站名称", "stationName", "80px")); config.add(new ColumnConfig("刀流水号", "serialNumber", "80px")); config.add(new ColumnConfig("主轴类型", "ladingType", "80px")); config.add(new ColumnConfig("机床号", "machineNo", "80px")); config.add(new ColumnConfig("创建日期", "creationDate", "80px")); config.add(new ColumnConfig("部门编号", "deptCode", "80px")); config.add(new ColumnConfig("部门名称", "deptName", "80px")); config.add(new ColumnConfig("领用人", "empNo", "80px")); config.add(new ColumnConfig("姓名", "empName", "80px")); config.add(new ColumnConfig("创建人", "createdBy", "80px")); config.add(new ColumnConfig("子库货位", "locatorFrom", "80px")); config.add(new ColumnConfig("刀具名称", "toolName", "80px")); config.add(new ColumnConfig("数量", "qty", "30px")); config.add(new ColumnConfig("刀具直径", "dia", "80px")); config.add(new ColumnConfig("刀具内径", "diaMin", "80px")); config.add(new ColumnConfig("加工深度", "deep", "80px")); config.add(new ColumnConfig("刀具编码", "itemCode", "200px")); config.add(new ColumnConfig("刀具名称", "itemDesc", "400px")); headerMap.put("config", config); //机床转移 List<ColumnConfig> move = new ArrayList<>(); move.add(new ColumnConfig("组织", "orgCode", "30px")); move.add(new ColumnConfig("申请单号", "requestNo", "120px")); move.add(new ColumnConfig("类型", "noteType", "80px")); move.add(new ColumnConfig("状态", "state", "80px")); move.add(new ColumnConfig("申请人", "createdBy", "60px")); move.add(new ColumnConfig("转出库位", "locatorFrom", "60px")); move.add(new ColumnConfig("转入库位", "locatorTo", "60px")); move.add(new ColumnConfig("刀具编码", "itemCode", "200px")); move.add(new ColumnConfig("刀具名称", "itemDesc", "400px")); move.add(new ColumnConfig("数量", "qty", "30px")); //move.add(new ColumnConfig("旧刀汇总编号", "oldToolGatherNo", "60px")); headerMap.put("move", move); //个人帐转移 List<ColumnConfig> personTransfer = new ArrayList<>(); personTransfer.add(new ColumnConfig("组织", "orgCode", "30px")); personTransfer.add(new ColumnConfig("申请单号", "requestNo", "80px")); personTransfer.add(new ColumnConfig("类型", "noteType", "40px")); personTransfer.add(new ColumnConfig("状态", "state", "40px")); personTransfer.add(new ColumnConfig("转出人", "empNo", "60px")); personTransfer.add(new ColumnConfig("转出机床", "machineNo", "60px")); personTransfer.add(new ColumnConfig("创建日期", "creationDate", "80px")); personTransfer.add(new ColumnConfig("部门编号", "deptCode", "80px")); personTransfer.add(new ColumnConfig("部门名称", "deptName", "80px")); //personTransfer.add(new ColumnConfig("旧刀汇总编号", "oldToolGatherNo", "60px")); headerMap.put("personTransfer", personTransfer); //退刀 List<ColumnConfig> returnTool = new ArrayList<>(); returnTool.add(new ColumnConfig("组织", "orgCode", "40px")); returnTool.add(new ColumnConfig("申请单号", "requestNo", "100px")); returnTool.add(new ColumnConfig("刀具站名称", "stationName", "120px")); returnTool.add(new ColumnConfig("机床号", "machineNo", "80px")); returnTool.add(new ColumnConfig("创建日期", "creationDate", "120px")); returnTool.add(new ColumnConfig("部门编号", "deptCode", "60px")); returnTool.add(new ColumnConfig("部门名称", "deptName", "80px")); returnTool.add(new ColumnConfig("领用人", "empNo", "60px")); returnTool.add(new ColumnConfig("姓名", "empName", "60px")); returnTool.add(new ColumnConfig("创建人", "createdBy", "60px")); returnTool.add(new ColumnConfig("子库货位", "locatorFrom", "60px")); returnTool.add(new ColumnConfig("刀具名称", "toolName", "160px")); returnTool.add(new ColumnConfig("完全可用", "useableQty", "50px")); returnTool.add(new ColumnConfig("不可用待挑选", "unUseableQty", "80px")); returnTool.add(new ColumnConfig("损坏", "demageQty", "30px")); returnTool.add(new ColumnConfig("打飞", "offQty", "30px")); returnTool.add(new ColumnConfig("数量", "qty", "30px")); returnTool.add(new ColumnConfig("刀具直径", "dia", "60px")); returnTool.add(new ColumnConfig("刀具内径", "diaMin", "60px")); returnTool.add(new ColumnConfig("加工深度", "deep", "60px")); headerMap.put("returnTool", returnTool); //损耗退仓 List<ColumnConfig> lessReturn = new ArrayList<>(); lessReturn.add(new ColumnConfig("组织", "orgCode", "30px")); lessReturn.add(new ColumnConfig("申请单号", "requestNo", "120px")); lessReturn.add(new ColumnConfig("类型", "noteType", "40px")); lessReturn.add(new ColumnConfig("填写时间", "creationDate", "80px")); lessReturn.add(new ColumnConfig("部门编号", "deptCode", "90px")); lessReturn.add(new ColumnConfig("部门名称", "deptName", "60px")); lessReturn.add(new ColumnConfig("开单人", "createdBy", "60px")); lessReturn.add(new ColumnConfig("状态", "state", "60px")); lessReturn.add(new ColumnConfig("货位", "locatorFrom", "40px")); lessReturn.add(new ColumnConfig("刀具编码", "itemCode", "180px")); lessReturn.add(new ColumnConfig("刀具名称", "itemDesc", "280px")); lessReturn.add(new ColumnConfig("数量", "qty", "20px")); lessReturn.add(new ColumnConfig("erp信息", "erpState", "60px")); headerMap.put("lessReturn", lessReturn); //不可用 List<ColumnConfig> unavailable = new ArrayList<>(); unavailable.add(new ColumnConfig("组织", "orgCode", "30px")); unavailable.add(new ColumnConfig("申请单号", "requestNo", "100px")); unavailable.add(new ColumnConfig("类型", "noteType", "60px")); unavailable.add(new ColumnConfig("刀具站名称", "stationName", "60px")); unavailable.add(new ColumnConfig("申请人", "empNo", "60px")); unavailable.add(new ColumnConfig("姓名", "empName", "60px")); unavailable.add(new ColumnConfig("刀具编码", "itemCode", "60px")); unavailable.add(new ColumnConfig("刀具名称", "itemDesc", "60px")); unavailable.add(new ColumnConfig("数量", "qty", "60px")); headerMap.put("unavailable", unavailable); List<ColumnConfig> unpaid = new ArrayList<>(); //借用未还 unpaid.add(new ColumnConfig("组织", "orgCode", "30px")); unpaid.add(new ColumnConfig("申请单号", "requestNo", "100px")); unpaid.add(new ColumnConfig("领用日期", "creationDate", "120px")); unpaid.add(new ColumnConfig("借领时长", "requestNo", "80px")); unpaid.add(new ColumnConfig("部门编号", "deptCode", "80px")); unpaid.add(new ColumnConfig("部门名称", "deptName", "80px")); unpaid.add(new ColumnConfig("申请人", "empNo", "60px")); unpaid.add(new ColumnConfig("姓名", "empName", "60px")); unpaid.add(new ColumnConfig("操作人", "lastUpdatedBy", "60px")); unpaid.add(new ColumnConfig("姓名", "lastUpdatedName", "60px")); unpaid.add(new ColumnConfig("子库货位", "locatorFrom", "60px")); unpaid.add(new ColumnConfig("刀具编码", "itemCode", "60px")); unpaid.add(new ColumnConfig("刀具名称", "itemDesc", "60px")); unpaid.add(new ColumnConfig("待还数量", "qty", "30px")); headerMap.put("unpaid", unpaid); //刀具站领用 List<ColumnConfig> stationHeader = new ArrayList<>(); stationHeader.add(new ColumnConfig("组织", "orgCode", "30px")); stationHeader.add(new ColumnConfig("申请单号", "requestNo", "120px")); stationHeader.add(new ColumnConfig("领用日期", "creationDate", "120px")); stationHeader.add(new ColumnConfig("员工编号", "empNo", "60px")); stationHeader.add(new ColumnConfig("员工姓名", "empName", "60px")); stationHeader.add(new ColumnConfig("部门编号", "deptCode", "60px")); stationHeader.add(new ColumnConfig("部门名称", "deptName", "60px")); stationHeader.add(new ColumnConfig("站点名称", "stationName", "60px")); stationHeader.add(new ColumnConfig("领用机床", "machineNo", "60px")); stationHeader.add(new ColumnConfig("刀具名称", "toolName", "60px")); stationHeader.add(new ColumnConfig("领用数量", "qty", "30px")); stationHeader.add(new ColumnConfig("领用类型", "type", "50px")); stationHeader.add(new ColumnConfig("货位", "locatorFrom", "60px")); stationHeader.add(new ColumnConfig("状态", "state", "60px")); headerMap.put("stationReceive", stationHeader); //刀具亭领用 List<ColumnConfig> storeHeader = new ArrayList<>(); storeHeader.add(new ColumnConfig("组织", "orgCode", "30px")); storeHeader.add(new ColumnConfig("申请单号", "requestNo", "80px")); storeHeader.add(new ColumnConfig("领用日期", "creationDate", "120px")); storeHeader.add(new ColumnConfig("员工编号", "empNo", "60px")); storeHeader.add(new ColumnConfig("员工姓名", "empName", "60px")); storeHeader.add(new ColumnConfig("部门编号", "deptCode", "60px")); storeHeader.add(new ColumnConfig("部门名称", "deptName", "60px")); storeHeader.add(new ColumnConfig("站点名称", "stationName", "60px")); storeHeader.add(new ColumnConfig("领用机床", "machineNo", "60px")); storeHeader.add(new ColumnConfig("刀具编码", "itemCode", "120px")); storeHeader.add(new ColumnConfig("刀具名称", "itemDesc", "240px")); storeHeader.add(new ColumnConfig("领用数量", "qty", "30px")); storeHeader.add(new ColumnConfig("领用类型", "type", "50px")); storeHeader.add(new ColumnConfig("货位", "locatorFrom", "60px")); storeHeader.add(new ColumnConfig("以旧换新", "oldToNew", "60px")); storeHeader.add(new ColumnConfig("状态", "state", "60px")); headerMap.put("storeReceive", storeHeader); return headerMap; } @PostConstruct public void init() { try { if (serverInfoBean == null) { serverInfoBean = (ServerInfoBean) FacesUtils.findViewBean("serverInfoBean"); } toolNameInit(); query = new NoteRequestReportQuery(); setOrg(); query.setReportCode("personTransfer"); columnFields = header().get("personTransfer"); noteRequestList = new LazyDataModel() { @Override public List load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) { if (!doQuery) { return Collections.emptyList(); } Map<String, String> queryMap = query.getQueryMap(); Integer total = 0; if (StringUtils.equals(query.getReportCode(), "config")) { requestDmList = noteRequestEjb.queryEmpConfig(first, pageSize, queryMap); total = noteRequestEjb.queryEmpConfigCount(queryMap); } else if (StringUtils.equals(query.getReportCode(), "personTransfer")) { requestDmList = noteRequestEjb.queryNoteReqDistinct(first, pageSize, queryMap, null); total = noteRequestEjb.getNoteReqUnApprCnt(queryMap); } else { requestDmList = noteRequestEjb.queryNoteReqByPage(first, pageSize, queryMap, new HashMap<String, String>()); total = noteRequestEjb.getNoteReqCount(queryMap); for (ToolNoteRequestDm toolNoteRequestDm : requestDmList) { HashMap<String, String> checkQuery = new HashMap<>(); checkQuery.put("requestNo", toolNoteRequestDm.getRequestNo()); List<ToolCheckDm> toolCheckDms = toolCheckEjb.queryCheckByPage(first, pageSize, checkQuery, new HashMap<>()); for (ToolCheckDm toolCheckDm : toolCheckDms) { toolNoteRequestDm.setUseableQty(toolCheckDm.getUseableQty()); toolNoteRequestDm.setUnUseableQty(toolCheckDm.getUnUseableQty()); toolNoteRequestDm.setDemageQty(toolCheckDm.getDemageQty()); toolNoteRequestDm.setOffQty(toolCheckDm.getOffQty()); } } } requestDmList = afterHandle(requestDmList, query); this.setRowCount(total.intValue()); return requestDmList; } }; initRole(); } catch (Exception e) { FacesUtils.showError("错误", ExceptionUtils.getRootCauseMessage(e)); } } private List<ToolNoteRequestDm> afterHandle(List<ToolNoteRequestDm> requestDmList, NoteRequestReportQuery query) { if (StringUtils.equals(query.getReportCode(), "returnTool") && StringUtils.equals(query.getMotive(), "刀具站")) { return returnDistinct(requestDmList); } return requestDmList; } private List<ToolNoteRequestDm> returnDistinct(List<ToolNoteRequestDm> requestDmList) { List<ToolNoteRequestDm> resultList = new ArrayList<>(); Map<String, ToolNoteRequestDm> reqMap = new HashMap<>(); for (ToolNoteRequestDm reqDm : requestDmList) { ToolNoteRequestDm dm = reqMap.get(reqDm.getRequestNo()); if (null == dm) { reqMap.put(reqDm.getRequestNo(), reqDm); continue; } dm.setQty(dm.getQty() + reqDm.getQty()); } resultList.addAll(reqMap.values()); return resultList; }
07-22
这段代码是怎么渲染表头的?, @PostConstruct public void init() { try { if (serverInfoBean == null) { serverInfoBean = (ServerInfoBean) FacesUtils.findViewBean("serverInfoBean"); } toolNameInit(); query = new NoteRequestReportQuery(); setOrg(); query.setReportCode("personTransfer"); columnFields = header().get("personTransfer"); noteRequestList = new LazyDataModel() { @Override public List load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) { if (!doQuery) { return Collections.emptyList(); } Map<String, String> queryMap = query.getQueryMap(); Integer total = 0; if (StringUtils.equals(query.getReportCode(), "config")) { logger.info("我是1"); requestDmList = noteRequestEjb.queryEmpConfig(first, pageSize, queryMap); total = noteRequestEjb.queryEmpConfigCount(queryMap); } else if (StringUtils.equals(query.getReportCode(), "personTransfer")) { logger.info("我是2"); requestDmList = noteRequestEjb.queryNoteReqDistinct(first, pageSize, queryMap, null); total = noteRequestEjb.getNoteReqUnApprCnt(queryMap); } else { requestDmList = noteRequestEjb.queryNoteReqByPage(first, pageSize, queryMap, new HashMap<String, String>()); total = noteRequestEjb.getNoteReqCount(queryMap); for (ToolNoteRequestDm toolNoteRequestDm : requestDmList) { logger.info(toolNoteRequestDm.getRequestNo()+",我是3:"+toolNoteRequestDm.getUnUseableQty()); } } logger.info("我是4"); requestDmList = afterHandle(requestDmList, query); this.setRowCount(total.intValue()); return requestDmList; } }; initRole(); } catch (Exception e) { FacesUtils.showError("错误", ExceptionUtils.getRootCauseMessage(e)); } }, public Map<String, List<ColumnConfig>> header() { Map<String, List<ColumnConfig>> headerMap = new HashMap<>(); // 机床配置报表 List<ColumnConfig> config = new ArrayList<>(); config.add(new ColumnConfig("组织", "orgCode", "30px")); config.add(new ColumnConfig("申请单号", "requestNo", "120px")); config.add(new ColumnConfig("刀具站名称", "stationName", "80px")); config.add(new ColumnConfig("刀流水号", "serialNumber", "80px")); config.add(new ColumnConfig("主轴类型", "ladingType", "80px")); config.add(new ColumnConfig("机床号", "machineNo", "80px")); config.add(new ColumnConfig("创建日期", "creationDate", "80px")); config.add(new ColumnConfig("部门编号", "deptCode", "80px")); config.add(new ColumnConfig("部门名称", "deptName", "80px")); config.add(new ColumnConfig("领用人", "empNo", "80px")); config.add(new ColumnConfig("姓名", "empName", "80px")); config.add(new ColumnConfig("创建人", "createdBy", "80px")); config.add(new ColumnConfig("子库货位", "locatorFrom", "80px")); config.add(new ColumnConfig("刀具名称", "toolName", "80px"));//* config.add(new ColumnConfig("完全可用", "useableQty ", "80px")); config.add(new ColumnConfig("不可用待挑选", "unUseableQty ", "80px")); config.add(new ColumnConfig("损坏", "demageQty ", "80px")); config.add(new ColumnConfig("打飞", "offQty ", "80px")); config.add(new ColumnConfig("数量", "qty", "30px"));//* config.add(new ColumnConfig("刀具直径", "dia", "80px")); config.add(new ColumnConfig("刀具内径", "diaMin", "80px")); config.add(new ColumnConfig("加工深度", "deep", "80px")); config.add(new ColumnConfig("刀具编码", "itemCode", "200px")); config.add(new ColumnConfig("刀具名称", "itemDesc", "400px")); headerMap.put("config", config); //机床转移 List<ColumnConfig> move = new ArrayList<>(); move.add(new ColumnConfig("组织", "orgCode", "30px")); move.add(new ColumnConfig("申请单号", "requestNo", "120px")); move.add(new ColumnConfig("类型", "noteType", "80px")); move.add(new ColumnConfig("状态", "state", "80px")); move.add(new ColumnConfig("申请人", "createdBy", "60px")); move.add(new ColumnConfig("转出库位", "locatorFrom", "60px")); move.add(new ColumnConfig("转入库位", "locatorTo", "60px")); move.add(new ColumnConfig("刀具编码", "itemCode", "200px")); move.add(new ColumnConfig("刀具名称", "itemDesc", "400px")); move.add(new ColumnConfig("数量", "qty", "30px")); //move.add(new ColumnConfig("旧刀汇总编号", "oldToolGatherNo", "60px")); headerMap.put("move", move); //个人帐转移 List<ColumnConfig> personTransfer = new ArrayList<>(); personTransfer.add(new ColumnConfig("组织", "orgCode", "30px")); personTransfer.add(new ColumnConfig("申请单号", "requestNo", "80px")); personTransfer.add(new ColumnConfig("类型", "noteType", "40px")); personTransfer.add(new ColumnConfig("状态", "state", "40px")); personTransfer.add(new ColumnConfig("转出人", "empNo", "60px")); personTransfer.add(new ColumnConfig("转出机床", "machineNo", "60px")); personTransfer.add(new ColumnConfig("创建日期", "creationDate", "80px")); personTransfer.add(new ColumnConfig("部门编号", "deptCode", "80px")); personTransfer.add(new ColumnConfig("部门名称", "deptName", "80px")); //personTransfer.add(new ColumnConfig("旧刀汇总编号", "oldToolGatherNo", "60px")); headerMap.put("personTransfer", personTransfer); //退刀 List<ColumnConfig> returnTool = new ArrayList<>(); returnTool.add(new ColumnConfig("组织", "orgCode", "40px")); returnTool.add(new ColumnConfig("申请单号", "requestNo", "100px")); returnTool.add(new ColumnConfig("刀具站名称", "stationName", "120px")); returnTool.add(new ColumnConfig("机床号", "machineNo", "80px")); returnTool.add(new ColumnConfig("创建日期", "creationDate", "120px")); returnTool.add(new ColumnConfig("部门编号", "deptCode", "60px")); returnTool.add(new ColumnConfig("部门名称", "deptName", "80px")); returnTool.add(new ColumnConfig("领用人", "empNo", "60px")); returnTool.add(new ColumnConfig("姓名", "empName", "60px")); returnTool.add(new ColumnConfig("创建人", "createdBy", "60px")); returnTool.add(new ColumnConfig("子库货位", "locatorFrom", "60px")); returnTool.add(new ColumnConfig("刀具名称", "toolName", "160px")); returnTool.add(new ColumnConfig("数量", "qty", "30px")); returnTool.add(new ColumnConfig("刀具直径", "dia", "60px")); returnTool.add(new ColumnConfig("刀具内径", "diaMin", "60px")); returnTool.add(new ColumnConfig("加工深度", "deep", "60px")); headerMap.put("returnTool", returnTool); //损耗退仓 List<ColumnConfig> lessReturn = new ArrayList<>(); lessReturn.add(new ColumnConfig("组织", "orgCode", "30px")); lessReturn.add(new ColumnConfig("申请单号", "requestNo", "120px")); lessReturn.add(new ColumnConfig("类型", "noteType", "40px")); lessReturn.add(new ColumnConfig("填写时间", "creationDate", "80px")); lessReturn.add(new ColumnConfig("部门编号", "deptCode", "90px")); lessReturn.add(new ColumnConfig("部门名称", "deptName", "60px")); lessReturn.add(new ColumnConfig("开单人", "createdBy", "60px")); lessReturn.add(new ColumnConfig("状态", "state", "60px")); lessReturn.add(new ColumnConfig("货位", "locatorFrom", "40px")); lessReturn.add(new ColumnConfig("刀具编码", "itemCode", "180px")); lessReturn.add(new ColumnConfig("刀具名称", "itemDesc", "280px")); lessReturn.add(new ColumnConfig("数量", "qty", "20px")); lessReturn.add(new ColumnConfig("erp信息", "erpState", "60px")); headerMap.put("lessReturn", lessReturn); //不可用 List<ColumnConfig> unavailable = new ArrayList<>(); unavailable.add(new ColumnConfig("组织", "orgCode", "30px")); unavailable.add(new ColumnConfig("申请单号", "requestNo", "100px")); unavailable.add(new ColumnConfig("类型", "noteType", "60px")); unavailable.add(new ColumnConfig("刀具站名称", "stationName", "60px")); unavailable.add(new ColumnConfig("申请人", "empNo", "60px")); unavailable.add(new ColumnConfig("姓名", "empName", "60px")); unavailable.add(new ColumnConfig("刀具编码", "itemCode", "60px")); unavailable.add(new ColumnConfig("刀具名称", "itemDesc", "60px")); unavailable.add(new ColumnConfig("数量", "qty", "60px")); headerMap.put("unavailable", unavailable); List<ColumnConfig> unpaid = new ArrayList<>(); //借用未还 unpaid.add(new ColumnConfig("组织", "orgCode", "30px")); unpaid.add(new ColumnConfig("申请单号", "requestNo", "100px")); unpaid.add(new ColumnConfig("领用日期", "creationDate", "120px")); unpaid.add(new ColumnConfig("借领时长", "requestNo", "80px")); unpaid.add(new ColumnConfig("部门编号", "deptCode", "80px")); unpaid.add(new ColumnConfig("部门名称", "deptName", "80px")); unpaid.add(new ColumnConfig("申请人", "empNo", "60px")); unpaid.add(new ColumnConfig("姓名", "empName", "60px")); unpaid.add(new ColumnConfig("操作人", "lastUpdatedBy", "60px")); unpaid.add(new ColumnConfig("姓名", "lastUpdatedName", "60px")); unpaid.add(new ColumnConfig("子库货位", "locatorFrom", "60px")); unpaid.add(new ColumnConfig("刀具编码", "itemCode", "60px")); unpaid.add(new ColumnConfig("刀具名称", "itemDesc", "60px")); unpaid.add(new ColumnConfig("待还数量", "qty", "30px")); headerMap.put("unpaid", unpaid); //刀具站领用 List<ColumnConfig> stationHeader = new ArrayList<>(); stationHeader.add(new ColumnConfig("组织", "orgCode", "30px")); stationHeader.add(new ColumnConfig("申请单号", "requestNo", "120px")); stationHeader.add(new ColumnConfig("领用日期", "creationDate", "120px")); stationHeader.add(new ColumnConfig("员工编号", "empNo", "60px")); stationHeader.add(new ColumnConfig("员工姓名", "empName", "60px")); stationHeader.add(new ColumnConfig("部门编号", "deptCode", "60px")); stationHeader.add(new ColumnConfig("部门名称", "deptName", "60px")); stationHeader.add(new ColumnConfig("站点名称", "stationName", "60px")); stationHeader.add(new ColumnConfig("领用机床", "machineNo", "60px")); stationHeader.add(new ColumnConfig("刀具名称", "toolName", "60px")); stationHeader.add(new ColumnConfig("领用数量", "qty", "30px")); stationHeader.add(new ColumnConfig("领用类型", "type", "50px")); stationHeader.add(new ColumnConfig("货位", "locatorFrom", "60px")); stationHeader.add(new ColumnConfig("状态", "state", "60px")); headerMap.put("stationReceive", stationHeader); //刀具亭领用 List<ColumnConfig> storeHeader = new ArrayList<>(); storeHeader.add(new ColumnConfig("组织", "orgCode", "30px")); storeHeader.add(new ColumnConfig("申请单号", "requestNo", "80px")); storeHeader.add(new ColumnConfig("领用日期", "creationDate", "120px")); storeHeader.add(new ColumnConfig("员工编号", "empNo", "60px")); storeHeader.add(new ColumnConfig("员工姓名", "empName", "60px")); storeHeader.add(new ColumnConfig("部门编号", "deptCode", "60px")); storeHeader.add(new ColumnConfig("部门名称", "deptName", "60px")); storeHeader.add(new ColumnConfig("站点名称", "stationName", "60px")); storeHeader.add(new ColumnConfig("领用机床", "machineNo", "60px")); storeHeader.add(new ColumnConfig("刀具编码", "itemCode", "120px")); storeHeader.add(new ColumnConfig("刀具名称", "itemDesc", "240px")); storeHeader.add(new ColumnConfig("领用数量", "qty", "30px")); storeHeader.add(new ColumnConfig("领用类型", "type", "50px")); storeHeader.add(new ColumnConfig("货位", "locatorFrom", "60px")); storeHeader.add(new ColumnConfig("以旧换新", "oldToNew", "60px")); storeHeader.add(new ColumnConfig("状态", "state", "60px")); headerMap.put("storeReceive", storeHeader); return headerMap; } // ColumnConfig 类定义 public static class ColumnConfig { private String header; private String property; private String width; private String textAlign; public ColumnConfig(String header, String property, String width) { this.header = header; this.property = property; this.width = width; } public ColumnConfig(String header, String property) { this.header = header; this.property = property; this.width = "120px"; } public void setHeader(String header) { this.header = header; } public String getHeader() { return header; } public void setProperty(String property) { this.property = property; } public String getProperty() { return property; } public void setWidth(String width) { this.width = width; } public String getWidth() { return width; } }
07-20
package com.adayo.service.atsmode.atsview; /** * Copyright (c) 2015 FORYOU GENERAL ELECTRONICS CO.,LTD. All Rights Reserved. */ import android.animation.Animator; import android.animation.ObjectAnimator; import android.content.Context; import android.graphics.RectF; import android.hardware.fgecomservice.V1_0.EMessageID; import android.hardware.fgecomservice.V1_0.IFgeComService; import android.hardware.fgecomservice.V1_0.Message; import android.media.AudioAttributes; import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.Handler; import android.os.RemoteException; import android.text.TextUtils; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.view.animation.LinearInterpolator; import android.widget.ImageView; import android.widget.TextView; import android.widget.VideoView; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.lifecycle.LifecycleRegistry; import com.adayo.proxy.infrastructure.sourcemng.Beans.AppConfigType; import com.adayo.proxy.infrastructure.sourcemng.Beans.SourceInfo; import com.adayo.proxy.infrastructure.sourcemng.Control.SrcMngAudioSwitchManager; import com.adayo.proxy.infrastructure.sourcemng.Control.SrcMngSwitchManager; import com.adayo.proxy.infrastructure.sourcemng.Interface.IAdayoAudioFocusChange; import com.adayo.proxy.setting.system.utils.LogUtil; import com.adayo.service.atsmode.R; import com.adayo.service.atsmode.atsconfig.ATSConfig; import com.airbnb.lottie.LottieAnimationView; import com.jakewharton.rxbinding4.view.RxView; import com.jeremyliao.liveeventbus.LiveEventBus; import org.jetbrains.annotations.NotNull; import org.json.JSONObject; import java.io.IOException; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.concurrent.TimeUnit; import autodispose2.AutoDispose; import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider; import static com.adayo.service.atsmode.atsconfig.ATSConfig.ATS_MODE_WADE; import static com.adayo.service.atsmode.atsconfig.ATSConfig.ATS_TAG; import static com.adayo.service.atsmode.atsconfig.ATSConfig.CONFIG_CAR_MODEL_B41V; import static com.adayo.service.atsmode.atsconfig.ATSConfig.CONFIG_CAR_MODEL_B41VS; import static com.adayo.service.atsmode.atsconfig.ATSConfig.CONFIG_CAR_MODEL_B60VS; import static com.adayo.service.atsmode.atsconfig.ATSConfig.EVENT_ATS_RESET_COUNTER; import static com.adayo.service.atsmode.atsconfig.ATSConfig.FLAG_OPERATE_UPDATE; import static com.adayo.service.atsmode.atsconfig.ATSConfig.MODEL_CONFIGURATION_VERSION_C; import static com.adayo.service.atsmode.atsconfig.ATSConfig.MODEL_CONFIGURATION_VERSION_P; /** * @ClassName: ATSContainerView * @Description: ATS驾驶模式容器view * @Author: yongqiang.wen@foryouge.com.cn * @CreateDate: 2022/12/13 14:03 */ public class ATSContainerView extends ConstraintLayout { SvgPathView svgPathView = null; VideoView videoView = null; TextView atsModeName = null; View atsToastTipsContainer = null; View atsToastIconContainer = null; ImageView atsLoadingView = null; ImageView atsErrorIcon = null; TextView atsToastTips = null; LottieAnimationView lottieAnimationView = null; String currentPathName = ""; String requestedModeName = ""; String confirmModeName = ""; private OnATSViewCloseListener closeListener = null; // 生命周期感知注册 private LifecycleRegistry lifecycleRegistry = null; // ATS驾驶模式ID对应关系表 private HashMap<Integer, String> pathIds = new HashMap<>(); // 用于查找驾驶模式中英文对照 // private HashMap<String, String> pathMaps = new HashMap<>(); // 加载下一个模式,用于判断是否播放剩余的50%动效 private boolean notYetSwitchNext = false; // 信号值标记,用于记录是否有信号过来 private int operateFlag; // 上一个模式记录 private String previousModePathName; // ATS驾驶模式关闭按钮 private ImageView atsClose; // 型号配置字 int carModelConfig = -1; // 模式配置字 int configuration = -1; // 是否有效信号 private boolean isValidSignal = false; private IFgeComService mIFgeComService = null; private MediaPlayer mediaPlayer; public ATSContainerView(Context context) { this(context, null); } public ATSContainerView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ATSContainerView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public void initATSView() { // 初始化子View initChildView(); // 初始化视频播放器 initVideoView(); // 初始化连接服务 initComService(); } private void initComService() { if (mIFgeComService == null) { try { mIFgeComService = IFgeComService.getService(); } catch (RemoteException e) { e.printStackTrace(); } } } public int getCarModelConfig() { return carModelConfig; } public void setCarModelConfig(int carModelConfig) { this.carModelConfig = carModelConfig; initATSSrcData(carModelConfig); if (svgPathView != null && !svgPathView.hasInitialize()) { svgPathView.init(); } LogUtil.e(ATSConfig.ATS_TAG, "ATSContainerView init carModelConfig byte value is ====>>" + carModelConfig); } public void initATSSrcData(int carModelConfig) { if (carModelConfig == CONFIG_CAR_MODEL_B41V) { // 生成41V模式数据 genAtsModeData(); } else if (carModelConfig == CONFIG_CAR_MODEL_B60VS || carModelConfig == CONFIG_CAR_MODEL_B41VS) { // 生成60VS模式数据 gen60VAtsModeData(); } } public int getConfiguration() { return configuration; } public void setConfiguration(int configuration) { this.configuration = configuration; LogUtil.e(ATSConfig.ATS_TAG, "ATSContainerView init configuration byte value is ====>>" + configuration); } private void initChildView() { svgPathView = getRootView().findViewById(R.id.svg_view); videoView = getRootView().findViewById(R.id.video_view); atsModeName = getRootView().findViewById(R.id.tv_ats_mode_name); atsClose = getRootView().findViewById(R.id.iv_ats_close); atsToastTipsContainer = getRootView().findViewById(R.id.toast_ats_mode_enter_tips); atsToastIconContainer = getRootView().findViewById(R.id.view_icon_container); atsLoadingView = getRootView().findViewById(R.id.iv_toast_loading); atsErrorIcon = getRootView().findViewById(R.id.iv_toast_icon); atsToastTips = getRootView().findViewById(R.id.tv_toast_msg); RxView.clicks(atsClose) .throttleFirst(500, TimeUnit.MILLISECONDS) .to(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleRegistry))) .subscribe(unit -> closeListener.onClose()); } public OnATSViewCloseListener getCloseListener() { return closeListener; } public void setCloseListener(OnATSViewCloseListener closeListener) { this.closeListener = closeListener; } int showMode = -1; private void initVideoView() { videoView.setOnCompletionListener(MediaPlayer::stop); videoView.setOnPreparedListener(mp -> { // 设置MediaPlayer不需要音频焦点 // mp.setAudioAttributes(new AudioAttributes.Builder() // .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) // .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) // .build()); // 判断切换模式动效View是否显示,进行隐藏 if (lottieAnimationView.getVisibility() == View.VISIBLE) { lottieAnimationView.setVisibility(View.INVISIBLE); atsModeName.setVisibility(View.VISIBLE); atsModeName.setText(getATSModeName(currentPathName)); } }); } private void handlingSpecialModeSignals() { showMode = -1; switch (currentPathName) { case "wad": if (carModelConfig == CONFIG_CAR_MODEL_B41V) { showMode = ATSConfig.ATS_MODE_WADE; } else if (carModelConfig == CONFIG_CAR_MODEL_B60VS || carModelConfig == CONFIG_CAR_MODEL_B41VS) { showMode = ATSConfig.ATS60_MODE_WAD; } break; case "rock": if (carModelConfig == CONFIG_CAR_MODEL_B41V) { showMode = ATSConfig.ATS_MODE_ROCK; } else if (carModelConfig == CONFIG_CAR_MODEL_B60VS || carModelConfig == CONFIG_CAR_MODEL_B41VS) { showMode = ATSConfig.ATS60_MODE_ROCK; } break; } if (showMode == ATSConfig.ATS_MODE_ROCK) { LogUtil.e(ATS_TAG, "ready to send enter rock mode signal to QNX"); } LiveEventBus.get(EVENT_ATS_RESET_COUNTER, Integer.class).post(showMode); // postDelayed(() -> { // if (showMode == ATSConfig.ATS_MODE_WADE && isValidSignal) { // String wadeTips = getContext().getString(R.string.tips_enter_the_water_depth_detection); // showModeEnterToast(wadeTips, true); // } else if (showMode == ATSConfig.ATS_MODE_ROCK && isValidSignal) { // String rockTips = getContext().getString(R.string.tips_enter_the_transparent_chassis); // showModeEnterToast(rockTips, true); // } // removeCallbacks(hideCustomToastRunnable); // postDelayed(hideCustomToastRunnable, 3000); // }, 2000); } private void hideCustomToast() { if (atsToastTipsContainer.getVisibility() == VISIBLE) { atsToastTipsContainer.setVisibility(GONE); } } private ObjectAnimator rotationAnimator = null; public void showModeEnterToast(String tips, boolean showLoading) { if (atsToastTipsContainer.getVisibility() == GONE) { atsToastTipsContainer.setVisibility(VISIBLE); } if (showLoading) { atsToastIconContainer.setVisibility(VISIBLE); atsLoadingView.setVisibility(VISIBLE); atsErrorIcon.setVisibility(GONE); // 创建旋转动画 if (rotationAnimator == null) { rotationAnimator = ObjectAnimator.ofFloat(atsLoadingView, "rotation", 0f, 360f); rotationAnimator.setDuration(1000); // 动画持续时间,单位为毫秒 rotationAnimator.setRepeatCount(ObjectAnimator.INFINITE); // 无限重复 rotationAnimator.setInterpolator(new LinearInterpolator()); // 线性插值器,使旋转平滑 // 启动动画 rotationAnimator.start(); } } else { atsToastIconContainer.setVisibility(GONE); atsLoadingView.setVisibility(GONE); } atsToastTipsContainer.setBackground(getResources().getDrawable(R.drawable.customize_toast_bg)); atsToastTips.setTextColor(getResources().getColor(R.color.drawable_dialog_text_color)); atsToastTips.setText(tips); } public void showErrorToast() { removeCallbacks(hideCustomToastRunnable); if (atsToastTipsContainer.getVisibility() == GONE) { atsToastTipsContainer.setVisibility(VISIBLE); } atsToastIconContainer.setVisibility(VISIBLE); atsErrorIcon.setVisibility(VISIBLE); atsLoadingView.setVisibility(GONE); atsToastTips.setText(getContext().getString(R.string.fail)); } /** * @param * @return void * @method observeRequestedModeDisplay * @description 切换模式回调 * @date: 2022/12/13 14:56 * @author: yongqiang.wen@foryouge.com.cn */ public void observeRequestedModeDisplay(int signal) { if (videoView != null && videoView.isPlaying()) { videoView.stopPlayback(); videoView.setBackground(getBackground()); setVideoViewVisibility(GONE); } setSvgPathViewVisibility(VISIBLE); requestedModeName = pathIds.get(signal); isValidSignal = false; if (checkInvalidSignal(signal)) return; isValidSignal = true; LogUtil.e(ATS_TAG, "The ATS mode requested signal has been received, current signal ===>> " + signal); requestedModeName = pathIds.get(signal); currentPathName = requestedModeName; // 如果上一个模式记录为空,则记录本次切换的模式路径 if (TextUtils.isEmpty(previousModePathName)) previousModePathName = currentPathName; LogUtil.d(ATS_TAG, "The ATS mode requested signal has been received, current mode ===>> " + currentPathName); atsModeName.setText(""); if (videoView.isPlaying()) { videoView.pause(); } atsModeName.setVisibility(View.GONE); if (lottieAnimationView != null && lottieAnimationView.isAnimating()) { // 处理当动效未结束,快速切换的问题 notYetSwitchNext = false; if (svgPathView.getSelect() != null) { previousModePathName = svgPathView.getSelect().getId(); } } if (signal >= 0 && signal < 12 || signal == 15) { // 由于目前41和60都是同样的模式范围,如果后续有变动,此处需要变更 showSwitchTips(); } removeCallbacks(hideCustomToastRunnable); switchNextMode(currentPathName); } private void showSwitchTips() { // atsToastTips.setVisibility(VISIBLE); String modeName = getATSModeName(currentPathName); String tipsTxt = getContext().getString(R.string.tips_switch_mode_txt, modeName); LogUtil.e(ATS_TAG, "The ATS mode requested signal has been received, current mode ===>> " + currentPathName); LogUtil.e(ATS_TAG, "The ATS mode requested signal has been received, current mode ===>> " + modeName + ", tipsTxt ===>>" + tipsTxt); showModeEnterToast(tipsTxt, false); // atsModeEnterTips.setText(tipsTxt); } /** * ATS岩石模式切换 发送给qnx * * @param rockModeState state */ public void updateRockModeStatus(int rockModeState) { LogUtil.i(ATS_TAG, "rockModeState = " + rockModeState); String id = "atsstatus"; String sub = ""; //装入json try { JSONObject command = new JSONObject(); command.put("id", id); command.put("sub", sub); JSONObject content = new JSONObject(); content.put("status", rockModeState); JSONObject all = new JSONObject(); all.put("command", command); all.put("content", content); String all_message = all.toString(); // LogUtil.i(ATS_TAG, "all_message = " + all_message); Message msg = new Message(); //看协议ID:device_070,然后看源码文件types.hal查看ID msg.id = EMessageID.MSG_DEVICE_ATSSTATUS; msg.content = all_message; LogUtil.i(ATS_TAG, "msg send = " + msg.content); if (mIFgeComService != null) { mIFgeComService.fgeSendMessage2ComService(msg); } } catch (Exception e) { LogUtil.e(ATS_TAG, "e = " + e.toString()); } } /** * @param * @return void * @method observeConfirmedModeDisplay * @description 确认模式回调 * @date: 2022/12/13 14:57 * @author: yongqiang.wen@foryouge.com.cn */ public boolean observeConfirmedModeDisplay(int signal) { if (checkInvalidSignal(signal)) return false; confirmModeName = pathIds.get(signal); atsModeName.setVisibility(View.GONE); LogUtil.d(ATS_TAG, "The ATS mode confirmation signal has been received,current mode ===>>" + getATSModeName(currentPathName)); if (TextUtils.equals(requestedModeName, confirmModeName)) { post(() -> runSwitchModeDisplayVideo(confirmModeName)); // atsClose.setVisibility(VISIBLE); String modeName = getATSModeName(confirmModeName); LogUtil.e(ATS_TAG, "setting current modeName ==>> " + modeName); atsModeName.setText(modeName); atsModeName.setVisibility(View.VISIBLE); queryOtherCMD(); return true; } return false; } public void setSvgPathViewVisibility(int visibility) { LogUtil.e(ATS_TAG, "setSvgPathViewVisibility called, visibility = " + visibility); if (svgPathView != null) { svgPathView.setVisibility(visibility); } if (atsClose != null) { atsClose.setVisibility(visibility); } if (atsModeName != null && atsModeName.getVisibility() == VISIBLE) { atsModeName.setVisibility(INVISIBLE); } } public void setVideoViewVisibility(int visibility) { LogUtil.e(ATS_TAG, "setVideoViewVisibility called, visibility = " + visibility); if (videoView != null) { videoView.setVisibility(visibility); } if (atsClose != null) { atsClose.setVisibility(visibility); } if (atsModeName != null) { atsModeName.setVisibility(visibility); } } private boolean checkInvalidSignal(int signal) { if (videoView.getVisibility() == VISIBLE) { setVideoViewVisibility(GONE); } if (carModelConfig == CONFIG_CAR_MODEL_B41V) { return signal == ATSConfig.ATS_MODE_RESERVED1 || signal == ATSConfig.ATS_MODE_RESERVED2; // if (signal == ATSConfig.ATS_MODE_FAULT || signal == ATSConfig.ATS_MODE_NOT_INITIALIZED) { // showErrorToast(); // return true; // } } else if (carModelConfig == CONFIG_CAR_MODEL_B60VS || carModelConfig == CONFIG_CAR_MODEL_B41VS) { return signal == ATSConfig.ATS60_MODE_SINGLE_PEDAL || signal == ATSConfig.ATS60_MODE_RAPIDLY || signal == ATSConfig.ATS60_MODE_RESERVED1 || signal == ATSConfig.ATS60_MODE_NOT_INITIALIZED; // if (signal == ATSConfig.ATS60_MODE_FAULT || signal == ATSConfig.ATS60_MODE_NOT_INITIALIZED) { // showErrorToast(); // return true; // } } return false; } /** * 特定模式下需要额外处理开启某功能,比如涉水模式要切换到涉水信息界面 */ private void queryOtherCMD() { if (!TextUtils.equals(currentPathName, pathIds.get(ATSConfig.ATS_MODE_ROCK))) { // 非岩石模式需要给QNX发送退出岩石模式信号 LogUtil.e(ATS_TAG, "ready to send exit rock mode signal to QNX"); updateRockModeStatus(ATSConfig.EXTRA_ROCK_STATE_EXIT); } // if (TextUtils.equals(currentPathName, pathIds.get(ATS_MODE_WADE))) { // 涉水模式 需要打开涉水页面 // Map<String, String> map = new HashMap<>(); // map.put("source", "carsetting"); // map.put("dest", "wddc"); // SourceInfo sourceInfo = new SourceInfo("com.adayo.app.bcm", map, // AppConfigType.SourceSwitch.APP_ON.getValue(), // AppConfigType.SourceType.UI_AUDIO.getValue()); // SrcMngSwitchManager.getInstance().requestSwitchApp(sourceInfo); // } } public void resetPreviousModePathName() { previousModePathName = ""; } /** * 获取当前模式 * @param currentMode */ private String getATSModeName(String currentMode) { if (carModelConfig == CONFIG_CAR_MODEL_B60VS) { // 北汽B60VS车型 if (currentMode.equals("snow")) { currentMode = "snow_60"; } } int modeRes = getResources().getIdentifier(currentMode, "string", getContext().getPackageName()); String currentName = ""; if (modeRes != 0) { currentName = getContext().getString(modeRes); } return currentName; } /** * @param * @return void * @method genAtsModeData * @description 生成ATS驾驶模式数据关系表,用于切换不同模式定位path * @date: 2022/11/28 9:43 * @author: yongqiang.wen@foryouge.com.cn */ private void genAtsModeData() { // 生成ATSMode映射关系表 pathIds.clear(); pathIds.put(ATSConfig.ATS_MODE_ECO, "eco"); pathIds.put(ATSConfig.ATS_MODE_COMFORT, "conf"); pathIds.put(ATSConfig.ATS_MODE_AUTO, "auto"); pathIds.put(ATSConfig.ATS_MODE_SNOW, "snow"); pathIds.put(ATSConfig.ATS_MODE_DEEP_SNOW, "deep_snow"); pathIds.put(ATSConfig.ATS_MODE_CROSS, "cross"); pathIds.put(ATSConfig.ATS_MODE_SAND, "sand"); pathIds.put(ATSConfig.ATS_MODE_MUD, "mud"); pathIds.put(ATSConfig.ATS_MODE_SLIP_MUD, "mud"); pathIds.put(ATSConfig.ATS_MODE_ROCK, "rock"); pathIds.put(ATSConfig.ATS_MODE_WADE, "wad"); pathIds.put(ATSConfig.ATS_MODE_SPORT, "sport"); // 系统未初始化的时候不显示任何模式 pathIds.put(ATSConfig.ATS_MODE_NOT_INITIALIZED, ""); // 当系统连接失败或者错误时,默认显示‘舒适’模式 pathIds.put(ATSConfig.ATS_MODE_FAULT, "conf"); } /** * @param * @return void * @method genAtsModeData * @description 生成ATS驾驶模式数据关系表,用于切换不同模式定位path * @date: 2023/6/12 9:43 * @author: yongqiang.wen@foryouge.com.cn */ private void gen60VAtsModeData() { // 生成ATSMode映射关系表 pathIds.clear(); pathIds.put(ATSConfig.ATS60_MODE_ECO, "eco"); pathIds.put(ATSConfig.ATS60_MODE_COMFORT, "conf"); pathIds.put(ATSConfig.ATS60_MODE_LIGHT_SNOW, "snow"); pathIds.put(ATSConfig.ATS60_MODE_DEEP_SNOW, "snow"); // pathIds.put(ATSConfig.ATS60_MODE_SINGLE_PEDAL, "single_pedal"); pathIds.put(ATSConfig.ATS60_MODE_RAPIDLY, "rapidly"); pathIds.put(ATSConfig.ATS60_MODE_THROUGH, "cross"); pathIds.put(ATSConfig.ATS60_MODE_SAND, "sand"); pathIds.put(ATSConfig.ATS60_MODE_DEEP_MUD, "mud"); pathIds.put(ATSConfig.ATS60_MODE_SHALLOW_MUD, "mud"); pathIds.put(ATSConfig.ATS60_MODE_ROCK, "rock"); pathIds.put(ATSConfig.ATS60_MODE_WAD, "wad"); pathIds.put(ATSConfig.ATS60_MODE_SPORT, "sport"); // 系统未初始化的时候不显示任何模式 pathIds.put(ATSConfig.ATS_MODE_NOT_INITIALIZED, ""); // 当系统连接失败或者错误时,默认显示‘舒适’模式 pathIds.put(ATSConfig.ATS_MODE_FAULT, "conf"); } private void switchNextMode(String pathName) { if (TextUtils.isEmpty(pathName)) return; if (lottieAnimationView == null) { ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lottieAnimationView = new LottieAnimationView(getContext()); // lottieAnimationView.addAnimatorListener(animatorListener); lottieAnimationView.addAnimatorUpdateListener(animation -> lottieAnimationView.setProgress(0.5f)); addView(lottieAnimationView, layoutParams); } lottieAnimationView.setVisibility(View.VISIBLE); runLottieAnim(pathName); } private final Animator.AnimatorListener animatorListener = new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { LogUtil.d(ATS_TAG, "animation start!" + animation.toString()); } @Override public void onAnimationEnd(Animator animation) { // 当上一个记录的模式路径与当前不一致时代表模式已经切换, // 需要先播放上一个路径消失的动效再播放新模式的加载动效 // if (!TextUtils.equals(currentPathName, previousModePathName)) { // previousModePathName = currentPathName; // switchNextMode(currentPathName); // } else if (notYetSwitchNext) { // lottieAnimationView.setMinAndMaxProgress(0.5f, 1); // notYetSwitchNext = false; // } LogUtil.d(ATS_TAG, "animation end!"); } @Override public void onAnimationCancel(Animator animation) { LogUtil.d(ATS_TAG, "animation cancel!"); } @Override public void onAnimationRepeat(Animator animation) { LogUtil.d(ATS_TAG, "animation repeat!"); } }; private void runSwitchModeDisplayVideo(String pathName) { // 切换模式需要让未播放的视频暂停 videoView.pause(); String currentPathName = ""; if (carModelConfig == CONFIG_CAR_MODEL_B41V) { // 北汽B41V车型 currentPathName = getB41VModelPath(pathName); } else if (carModelConfig == CONFIG_CAR_MODEL_B41VS) { // 北汽B41VS车型 currentPathName = getB41VSModelPath(pathName); } else if (carModelConfig == CONFIG_CAR_MODEL_B60VS) { // 北汽B60VS车型 currentPathName = getB60VSModelPath(pathName); } if (TextUtils.isEmpty(currentPathName)) return; svgPathView.setPathSelect(pathName); // 根据path name通过资源文件获取对应的动效视频路径 int videoSrc = getResources().getIdentifier(currentPathName, "raw", getContext().getPackageName()); String videoPath = "android.resource://" + getContext().getPackageName() + "/" + videoSrc; post(() -> videoView.setVideoPath(videoPath)); LogUtil.d(ATS_TAG, "current Video path is " + videoPath + ",currentPathName is " + currentPathName); if (videoSrc == 0) { return; } post(() -> { String tipsStr = getTipsStringByConfig(); if (!TextUtils.equals(confirmModeName, pathIds.get(ATSConfig.ATS60_MODE_RAPIDLY))) { // 极速模式没有提示文案 showModeEnterToast(tipsStr, false); removeCallbacks(hideCustomToastRunnable); postDelayed(hideCustomToastRunnable, 5000); } else { hideCustomToast(); } videoView.setAudioFocusRequest(AudioManager.AUDIOFOCUS_NONE); // SrcMngAudioSwitchManager.getInstance().requestAdayoAudioFocus(3, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT, "com.adayo.service.atsmode", mFocusChange,getContext()); videoView.setBackground(null); //设置音频策略跟随ktv通道 videoView.setAudioAttributes(new AudioAttributes.Builder() .setUsage(43) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .build()); // videoView.setAudioAttributes(new AudioAttributes.Builder() // .setLegacyStreamType(3) // .build()); videoView.start(); setSvgPathViewVisibility(INVISIBLE); setVideoViewVisibility(VISIBLE); handlingSpecialModeSignals(); // mediaPlayer = new MediaPlayer(); // int videoSrc2 = getResources().getIdentifier("deepsnow", "raw", getContext().getPackageName()); // try { // mediaPlayer.setDataSource(getContext(), Uri.parse("android.resource://" + getContext().getPackageName() + "/" + videoSrc2)); // } catch (IOException e) { // e.printStackTrace(); // } // // mediaPlayer.setAudioAttributes(new AudioAttributes.Builder() // .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) // .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) // .build()); // // mediaPlayer.prepareAsync(); // mediaPlayer.setLooping(true); // mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { // @Override // public void onPrepared(MediaPlayer mediaPlayer) { // mediaPlayer.start(); // } // }); // // // // new Handler().postDelayed(new Runnable() { // @Override // public void run() { // mediaPlayer.stop(); // mediaPlayer.reset(); // } // },7000); }); } private String getTipsStringByConfig() { int tipsRes = 0; String tipsStr = ""; if (carModelConfig == CONFIG_CAR_MODEL_B41V) { // 北汽B41V车型 tipsRes = getResources().getIdentifier("tips41_" + confirmModeName, "string", getContext().getPackageName()); } else if (carModelConfig == CONFIG_CAR_MODEL_B41VS) { if (TextUtils.equals(confirmModeName, pathIds.get(ATSConfig.ATS60_MODE_RAPIDLY))) { return ""; } // tipsRes = getResources().getIdentifier("tips41vs_" + confirmModeName, "string", getContext().getPackageName()); tipsRes = getResources().getIdentifier("tips60_" + confirmModeName, "string", getContext().getPackageName()); } else if (carModelConfig == CONFIG_CAR_MODEL_B60VS) { if (TextUtils.equals(confirmModeName, pathIds.get(ATSConfig.ATS60_MODE_RAPIDLY))) { return ""; } tipsRes = getResources().getIdentifier("tips60_" + confirmModeName, "string", getContext().getPackageName()); } tipsStr = getContext().getString(tipsRes); return tipsStr; } @NotNull private String getB41VModelPath(String pathName) { String currentPathName; // 根据配置字来匹配展示的动效文件 if (configuration == MODEL_CONFIGURATION_VERSION_C) { LogUtil.d(ATS_TAG, "current model configuration is C Version"); currentPathName = pathName + "_c"; } else if (configuration == MODEL_CONFIGURATION_VERSION_P) { LogUtil.d(ATS_TAG, "current model configuration is P Version"); currentPathName = pathName + "_p"; } else { LogUtil.d(ATS_TAG, "can not require current model configuration"); currentPathName = pathName + "_p"; } return currentPathName; } @NotNull private String getB60VSModelPath(String pathName) { String currentPathName; // 根据配置字来匹配展示的动效文件 currentPathName = pathName + "_60vs"; return currentPathName; } @NotNull private String getB41VSModelPath(String pathName) { String currentPathName; // 鏍规嵁閰嶇疆瀛楁潵鍖归厤灞曠ず鐨勫姩鏁堟枃浠? currentPathName = pathName + "_41vs"; return currentPathName; } private final Runnable hideCustomToastRunnable = this::hideCustomToast; private void runLottieAnim(String pathName) { // 播放切换动效 RectF rectF = svgPathView.getSelectRectF(pathName); if (lottieAnimationView != null && rectF != null) { // // 切换其他模式要先播放之前模式的隐藏动效 // if (!TextUtils.equals(previousModePathName, pathName) && lottieAnimationView.getVisibility() == VISIBLE) { // lottieAnimationView.playAnimation(); // return; // } // 设置lottieView宽高 lottieAnimationView.getLayoutParams().width = (int) rectF.width(); lottieAnimationView.getLayoutParams().height = (int) rectF.height(); // 设置lottieView对应的位置坐标 lottieAnimationView.setX(rectF.centerX() - rectF.width() / 2); lottieAnimationView.setY(rectF.centerY() - rectF.height() / 2); String animSrc = getStringLottiePathSrc(pathName); if (TextUtils.isEmpty(animSrc)) return; LogUtil.d(ATS_TAG, "lottie res file path :" + animSrc); lottieAnimationView.setAnimation(animSrc); lottieAnimationView.setRepeatCount(0); lottieAnimationView.setSpeed(0f); lottieAnimationView.setFrame(16); // if (!notYetSwitchNext) { // // 由于UI给的动效文件是完整的动画过程,50%是动效加载并显示的过程,剩余的50% // lottieAnimationView.setMinAndMaxProgress(0.5f, 0.5f); // notYetSwitchNext = true; // } lottieAnimationView.playAnimation(); operateFlag = FLAG_OPERATE_UPDATE; } } @NotNull private String getStringLottiePathSrc(String pathName) { // 处理多语言的情况,目前只有中英文 Locale locale = getResources().getConfiguration().locale; String language = locale.getLanguage(); LogUtil.d(ATS_TAG, "current system language is: " + language); String animSrc = null; if (carModelConfig == CONFIG_CAR_MODEL_B41V) { // 根据path加载对应的Lottie动画资源文件 if (TextUtils.equals(language, "en")) { animSrc = pathName + "41_en.json"; } else { animSrc = pathName + "41.json"; } } else if (carModelConfig == CONFIG_CAR_MODEL_B41VS) { // 根据path加载对应的Lottie动效资源文件 if (TextUtils.equals(language, "en")) { animSrc = pathName + "60_en.json"; } else { animSrc = pathName + "60.json"; } } else if (carModelConfig == CONFIG_CAR_MODEL_B60VS) { // 根据path加载对应的Lottie动画资源文件 if (TextUtils.equals(language, "en")) { animSrc = pathName + "60_en.json"; } else { animSrc = pathName + "60.json"; } } return animSrc; } public LifecycleRegistry getLifecycleRegistry() { return lifecycleRegistry; } public void setLifecycleRegistry(LifecycleRegistry lifecycleRegistry) { this.lifecycleRegistry = lifecycleRegistry; } public interface OnATSViewCloseListener { void onClose(); } private static IAdayoAudioFocusChange mFocusChange = new IAdayoAudioFocusChange() { @Override public void onAdayoAudioOnGain() { } @Override public void onAdayoAudioOnLoss() { } @Override public void onAdayoAudioLossTransient() { } @Override public void onAdayoAudioLossTransientCanDuck() { } }; }
最新发布
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值